
<!DOCTYPE html>
<html>

	<head>
	<title>CAIJL's Blog</title>
	<meta charset="utf-8"/>
	<link rel="stylesheet" href="/font-awesome-4.7.0/css/font-awesome.min.css">

	<link rel="stylesheet" href="/bootstrap-3.3.7/css/bootstrap.min.css">
	<script src="/jquery-2.1.1/jquery.min.js"></script>
	<script src="/bootstrap-3.3.7/js/bootstrap.min.js"></script>

	<link rel="stylesheet" href="/css/bootstrap-slider.min.css">
	<script src="/js/bootstrap-slider.min.js"></script>
	
	<link rel="stylesheet" href="/css/main.css">
	<script src="/js/main.js"></script>
	
	<link rel="stylesheet" href="/css/cursor.css">
	<script src="/js/cursor.js"></script>
	
	
	<link rel="stylesheet" href="/css/code.css">
	<!--link rel="stylesheet" href="/css/markdown.css"-->
	
	<link rel="stylesheet" href="/css/head.css">

	<script src="/js/window.js"></script>
	<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
	</head>
<body>

	<link rel="stylesheet" href="/katex/katex.min.css" crossorigin="anonymous">
	<script src="/katex/katex.min.js" crossorigin="anonymous"></script>
	<script src="/katex/contrib/mathtex-script-type.min.js" defer></script>
	<script defer src="/katex/contrib/auto-render.min.js"crossorigin="anonymous"
	    onload="renderMathInElement(document.body);"></script>
	<script>document.addEventListener("DOMContentLoaded", function() {renderMathInElement(document.body,{"delimiters":[{left: "$", right: "$", display: false}]});});</script>
	<nav class="navbar navbar-default header card" role="navigation" style="width: 100%;">
	<div class="container-fluid"> 
	<div class="navbar-header">
		<button type="button" class="navbar-toggle" data-toggle="collapse"
				data-target="#example-navbar-collapse">
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
		</button>
		<a class="navbar-brand" href="/"><span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="bold">C</mi><mi mathvariant="bold">A</mi><mi mathvariant="bold">I</mi><mi mathvariant="bold">J</mi><msup><mi mathvariant="bold">L</mi><mo mathvariant="bold" lspace="0em" rspace="0em">′</mo></msup><mi mathvariant="bold">s</mi><mi mathvariant="bold">B</mi><mi mathvariant="bold">L</mi><mi mathvariant="bold">O</mi><mi mathvariant="bold">G</mi></mrow><annotation encoding="application/x-tex">\mathbf{CAIJL'sBLOG}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 0.751892em; vertical-align: 0em;"></span><span class="mord"><span class="mord mathbf">C</span><span class="mord mathbf">A</span><span class="mord mathbf">I</span><span class="mord mathbf">J</span><span class="mord"><span class="mord mathbf">L</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.751892em;"><span class="" style="top: -3.063em; margin-right: 0.05em;"><span class="pstrut" style="height: 2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathbf mtight">′</span></span></span></span></span></span></span></span></span><span class="mord mathbf">s</span><span class="mord mathbf">B</span><span class="mord mathbf">L</span><span class="mord mathbf">O</span><span class="mord mathbf">G</span></span></span></span></span></span></a>
	</div>
	<div class="collapse navbar-collapse" id="example-navbar-collapse">
		<ul class="nav navbar-nav" style="background-color:#fcfcfc">
			<li><a href="/"><i class="menu-item-icon fa fa-fw fa-home"></i>首页</a></li>
			<li><a href="/archives/"><i class="menu-item-icon fa fa-fw fa-archive"></i>文章</a></li>
			<li><a href="/tags/"><i class="menu-item-icon fa fa-fw fa-tags"></i>标签</a></li>
			<li><a href="/settings/"><i class="menu-item-icon fa fa-fw fa-cogs"></i>设置</a></li>
			<!--li><a href="/about/"><i class="menu-item-icon fa fa-fw fa-user"></i>关于</a></li-->
			<li><a href="/games/"><i class="menu-item-icon fa fa-fw fa-gamepad"></i>其它</a></li>
			

		</ul>
	</div>
	</div>
	</nav>
<div class="container">
	<div style="height: 60px;"></div>
	<div class="row" >
		<div class="col-md-8">
			<div class="panel panel-default card" style="padding: 10px;position: relative;">
                <img src='/img/bg/18.png' style="width:100%"/>
				<span style="position: absolute;left:15px;bottom:15px;width:90%;"><font class="view-text" style="color:#fcfcfc;font-size:25px">[营业日志]菜鸡の模板</font><br><a href="/tags/2020/" class="tag"><span  style="background-color: rgb(52, 152, 219);">2020</span></a></span>
			</div>
			<div class="panel panel-default card" style="padding: 10px;" id="main">
                <!--more-->
<p><a href="/article/netflow.html">网络流专栏</a></p>
<p><a href="/article/poly.html">多项式专栏</a></p>
<h1 id="io">IO</h1>
<h2 id="_1">快读</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐代码</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="cp">#include</span><span class="cpf">&lt;bits/stdc++.h&gt;</span><span class="cp"></span>
<span class="linenos" data-linenos=" 2 "></span><span class="cp">#define faster</span>
<span class="linenos" data-linenos=" 3 "></span><span class="k">namespace</span> <span class="n">in</span><span class="p">{</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="cp">#ifdef faster</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">21</span><span class="p">],</span><span class="o">*</span><span class="n">p1</span><span class="o">=</span><span class="n">buf</span><span class="p">,</span><span class="o">*</span><span class="n">p2</span><span class="o">=</span><span class="n">buf</span><span class="p">;</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">getc</span><span class="p">(){</span><span class="k">return</span> <span class="n">p1</span><span class="o">==</span><span class="n">p2</span><span class="o">&amp;&amp;</span><span class="p">(</span><span class="n">p2</span><span class="o">=</span><span class="p">(</span><span class="n">p1</span><span class="o">=</span><span class="n">buf</span><span class="p">)</span><span class="o">+</span><span class="n">fread</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">21</span><span class="p">,</span><span class="n">stdin</span><span class="p">),</span><span class="n">p1</span><span class="o">==</span><span class="n">p2</span><span class="p">)</span><span class="o">?</span><span class="nl">EOF</span><span class="p">:</span><span class="o">*</span><span class="n">p1</span><span class="o">++</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="cp">#else</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">getc</span><span class="p">(){</span><span class="k">return</span> <span class="nf">getchar</span><span class="p">();}</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="cp">#endif</span>
<span class="linenos" data-linenos="10 "></span>    <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="nc">T</span><span class="o">&gt;</span><span class="kr">inline</span> <span class="kt">void</span> <span class="n">read</span><span class="p">(</span><span class="n">T</span><span class="o">&amp;</span> <span class="n">t</span><span class="p">){</span>
<span class="linenos" data-linenos="11 "></span>        <span class="n">t</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="kt">int</span> <span class="n">f</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="kt">char</span> <span class="n">ch</span><span class="o">=</span><span class="n">getc</span><span class="p">();</span><span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="n">isdigit</span><span class="p">(</span><span class="n">ch</span><span class="p">)){</span><span class="k">if</span><span class="p">(</span><span class="n">ch</span><span class="o">==</span><span class="sc">&#39;-&#39;</span><span class="p">)</span><span class="n">f</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span><span class="n">ch</span><span class="o">=</span><span class="n">getc</span><span class="p">();}</span>
<span class="linenos" data-linenos="12 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">isdigit</span><span class="p">(</span><span class="n">ch</span><span class="p">)){</span><span class="n">t</span><span class="o">=</span><span class="n">t</span><span class="o">*</span><span class="mi">10</span><span class="o">+</span><span class="n">ch</span><span class="mi">-48</span><span class="p">;</span><span class="n">ch</span> <span class="o">=</span> <span class="n">getc</span><span class="p">();}</span><span class="k">if</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="n">t</span><span class="o">=-</span><span class="n">t</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="14 "></span>    <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="nc">T</span><span class="p">,</span><span class="k">typename</span><span class="p">...</span> <span class="n">Args</span><span class="o">&gt;</span> <span class="kr">inline</span> <span class="kt">void</span> <span class="n">read</span><span class="p">(</span><span class="n">T</span><span class="o">&amp;</span> <span class="n">t</span><span class="p">,</span> <span class="n">Args</span><span class="o">&amp;</span><span class="p">...</span> <span class="n">args</span><span class="p">){</span><span class="n">read</span><span class="p">(</span><span class="n">t</span><span class="p">);</span><span class="n">read</span><span class="p">(</span><span class="n">args</span><span class="p">...);}</span>
<span class="linenos" data-linenos="15 "></span><span class="p">}</span>
<span class="linenos" data-linenos="16 "></span><span class="k">namespace</span> <span class="n">out</span><span class="p">{</span>
<span class="linenos" data-linenos="17 "></span>    <span class="kt">char</span> <span class="n">buffer</span><span class="p">[</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">21</span><span class="p">];</span><span class="kt">int</span> <span class="n">p1</span><span class="o">=</span><span class="mi">-1</span><span class="p">;</span><span class="k">const</span> <span class="kt">int</span> <span class="n">p2</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">21</span><span class="p">)</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="18 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">flush</span><span class="p">(){</span><span class="n">fwrite</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">p1</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">stdout</span><span class="p">),</span><span class="n">p1</span><span class="o">=</span><span class="mi">-1</span><span class="p">;}</span>
<span class="linenos" data-linenos="19 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">putc</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span><span class="k">if</span><span class="p">(</span><span class="n">p1</span><span class="o">==</span><span class="n">p2</span><span class="p">)</span><span class="n">flush</span><span class="p">();</span><span class="n">buffer</span><span class="p">[</span><span class="o">++</span><span class="n">p1</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="p">;}</span>
<span class="linenos" data-linenos="20 "></span>    <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="nc">T</span><span class="o">&gt;</span><span class="kt">void</span> <span class="n">write</span><span class="p">(</span><span class="n">T</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
<span class="linenos" data-linenos="21 "></span>        <span class="k">static</span> <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">15</span><span class="p">];</span><span class="k">static</span> <span class="kt">int</span> <span class="n">len</span><span class="o">=</span><span class="mi">-1</span><span class="p">;</span><span class="k">if</span><span class="p">(</span><span class="n">x</span><span class="o">&gt;=</span><span class="mi">0</span><span class="p">){</span><span class="k">do</span><span class="p">{</span><span class="n">buf</span><span class="p">[</span><span class="o">++</span><span class="n">len</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="o">%</span><span class="mi">10</span><span class="o">+</span><span class="mi">48</span><span class="p">,</span><span class="n">x</span><span class="o">/=</span><span class="mi">10</span><span class="p">;}</span><span class="k">while</span> <span class="p">(</span><span class="n">x</span><span class="p">);}</span><span class="k">else</span><span class="p">{</span><span class="n">putc</span><span class="p">(</span><span class="sc">&#39;-&#39;</span><span class="p">);</span><span class="k">do</span> <span class="p">{</span><span class="n">buf</span><span class="p">[</span><span class="o">++</span><span class="n">len</span><span class="p">]</span><span class="o">=-</span><span class="p">(</span><span class="n">x</span><span class="o">%</span><span class="mi">10</span><span class="p">)</span><span class="o">+</span><span class="mi">48</span><span class="p">,</span><span class="n">x</span><span class="o">/=</span><span class="mi">10</span><span class="p">;}</span><span class="k">while</span><span class="p">(</span><span class="n">x</span><span class="p">);}</span>
<span class="linenos" data-linenos="22 "></span>        <span class="k">while</span> <span class="p">(</span><span class="n">len</span><span class="o">&gt;=</span><span class="mi">0</span><span class="p">)</span><span class="n">putc</span><span class="p">(</span><span class="n">buf</span><span class="p">[</span><span class="n">len</span><span class="p">]),</span><span class="o">--</span><span class="n">len</span><span class="p">;</span>
<span class="linenos" data-linenos="23 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="24 "></span><span class="p">}</span>
<span class="linenos" data-linenos="25 "></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
<span class="linenos" data-linenos="26 "></span>
<span class="linenos" data-linenos="27 "></span><span class="kt">signed</span> <span class="nf">main</span><span class="p">(){</span>
<span class="linenos" data-linenos="28 "></span>    <span class="n">out</span><span class="o">::</span><span class="n">flush</span><span class="p">();</span>
<span class="linenos" data-linenos="29 "></span>    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="30 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
<h1 id="_2">图论</h1>
<h2 id="spfa">SPFA</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">maxn</span><span class="o">=</span><span class="mi">10010</span><span class="p">;</span>
<span class="linenos" data-linenos=" 2 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">maxm</span><span class="o">=</span><span class="mi">500010</span><span class="p">;</span>
<span class="linenos" data-linenos=" 3 "></span><span class="k">struct</span> <span class="nc">spfa</span><span class="p">{</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="kt">int</span> <span class="n">s</span><span class="p">,</span><span class="n">t</span><span class="p">,</span><span class="n">c</span><span class="p">;</span><span class="kt">bool</span> <span class="n">vis</span><span class="p">[</span><span class="n">maxn</span><span class="p">];</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="kt">int</span> <span class="n">head</span><span class="p">[</span><span class="n">maxn</span><span class="p">],</span><span class="n">path</span><span class="p">[</span><span class="n">maxn</span><span class="p">];</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="k">struct</span> <span class="nc">node</span><span class="p">{</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="kt">int</span> <span class="n">nxt</span><span class="p">,</span><span class="n">to</span><span class="p">,</span><span class="n">w</span><span class="p">;</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="p">}</span><span class="n">a</span><span class="p">[</span><span class="n">maxm</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">];</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">relax</span><span class="p">(</span><span class="kt">int</span> <span class="n">u</span><span class="p">,</span><span class="kt">int</span> <span class="n">v</span><span class="p">,</span><span class="kt">int</span> <span class="n">w</span><span class="p">){</span>
<span class="linenos" data-linenos="10 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">path</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">&gt;</span><span class="n">path</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">+</span><span class="n">w</span><span class="p">){</span>
<span class="linenos" data-linenos="11 "></span>            <span class="n">path</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="n">path</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">+</span><span class="n">w</span><span class="p">;</span>
<span class="linenos" data-linenos="12 "></span>            <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>        <span class="p">}</span><span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="14 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="15 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">add</span><span class="p">(</span><span class="kt">int</span> <span class="n">u</span><span class="p">,</span><span class="kt">int</span> <span class="n">v</span><span class="p">,</span><span class="kt">int</span> <span class="n">w</span><span class="p">){</span><span class="n">a</span><span class="p">[</span><span class="n">c</span><span class="p">].</span><span class="n">to</span><span class="o">=</span><span class="n">v</span><span class="p">;</span><span class="n">a</span><span class="p">[</span><span class="n">c</span><span class="p">].</span><span class="n">w</span><span class="o">=</span><span class="n">w</span><span class="p">;</span><span class="n">a</span><span class="p">[</span><span class="n">c</span><span class="p">].</span><span class="n">nxt</span><span class="o">=</span><span class="n">head</span><span class="p">[</span><span class="n">u</span><span class="p">];</span><span class="n">head</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">=</span><span class="n">c</span><span class="o">++</span><span class="p">;}</span>
<span class="linenos" data-linenos="16 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">init</span><span class="p">(</span><span class="kt">int</span> <span class="n">_s</span><span class="p">,</span><span class="kt">int</span> <span class="n">_t</span><span class="p">){</span><span class="n">s</span><span class="o">=</span><span class="n">_s</span><span class="p">,</span><span class="n">t</span><span class="o">=</span><span class="n">_t</span><span class="p">;</span><span class="n">memset</span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">head</span><span class="p">);</span><span class="n">c</span><span class="o">=</span><span class="mi">1</span><span class="p">;}</span>
<span class="linenos" data-linenos="17 "></span>    <span class="kt">void</span> <span class="n">deal</span><span class="p">(){</span>
<span class="linenos" data-linenos="18 "></span>        <span class="kt">int</span> <span class="n">u</span><span class="p">,</span><span class="n">v</span><span class="p">,</span><span class="n">num</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">list</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">q</span><span class="p">;</span>
<span class="linenos" data-linenos="19 "></span>        <span class="n">memset</span><span class="p">(</span><span class="n">path</span><span class="p">,</span><span class="mh">0x3f</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">path</span><span class="p">);</span>
<span class="linenos" data-linenos="20 "></span>        <span class="n">memset</span><span class="p">(</span><span class="n">vis</span><span class="p">,</span><span class="nb">false</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">vis</span><span class="p">);</span>
<span class="linenos" data-linenos="21 "></span>        <span class="n">path</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">vis</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">=</span><span class="nb">true</span><span class="p">;</span><span class="n">q</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">s</span><span class="p">);</span><span class="n">num</span><span class="o">++</span><span class="p">;</span>
<span class="linenos" data-linenos="22 "></span>        <span class="k">while</span><span class="p">(</span><span class="o">!</span><span class="n">q</span><span class="p">.</span><span class="n">empty</span><span class="p">()){</span>
<span class="linenos" data-linenos="23 "></span>            <span class="n">u</span><span class="o">=</span><span class="n">q</span><span class="p">.</span><span class="n">front</span><span class="p">();</span><span class="n">q</span><span class="p">.</span><span class="n">pop_front</span><span class="p">();</span>
<span class="linenos" data-linenos="24 "></span>            <span class="n">num</span><span class="o">--</span><span class="p">;</span><span class="n">x</span><span class="o">-=</span><span class="n">path</span><span class="p">[</span><span class="n">u</span><span class="p">];</span>
<span class="linenos" data-linenos="25 "></span>            <span class="k">while</span><span class="p">(</span><span class="n">num</span><span class="o">&amp;&amp;</span><span class="n">path</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">&gt;</span><span class="n">x</span><span class="o">/</span><span class="n">num</span><span class="p">){</span><span class="n">q</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">u</span><span class="p">);</span><span class="n">u</span><span class="o">=</span><span class="n">q</span><span class="p">.</span><span class="n">front</span><span class="p">();</span><span class="n">q</span><span class="p">.</span><span class="n">pop_front</span><span class="p">();}</span>
<span class="linenos" data-linenos="26 "></span>            <span class="n">vis</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">=</span><span class="nb">false</span><span class="p">;</span>
<span class="linenos" data-linenos="27 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="n">head</span><span class="p">[</span><span class="n">u</span><span class="p">];</span><span class="n">i</span><span class="p">;</span><span class="n">i</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">nxt</span><span class="p">){</span>
<span class="linenos" data-linenos="28 "></span>                <span class="n">v</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">to</span><span class="p">;</span>
<span class="linenos" data-linenos="29 "></span>                <span class="k">if</span><span class="p">(</span><span class="n">relax</span><span class="p">(</span><span class="n">u</span><span class="p">,</span><span class="n">v</span><span class="p">,</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">w</span><span class="p">)</span><span class="o">&amp;&amp;!</span><span class="n">vis</span><span class="p">[</span><span class="n">v</span><span class="p">]){</span>
<span class="linenos" data-linenos="30 "></span>                    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">q</span><span class="p">.</span><span class="n">empty</span><span class="p">()</span><span class="o">&amp;&amp;</span><span class="n">path</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">&lt;</span><span class="n">path</span><span class="p">[</span><span class="n">q</span><span class="p">.</span><span class="n">front</span><span class="p">()])</span><span class="n">q</span><span class="p">.</span><span class="n">push_front</span><span class="p">(</span><span class="n">v</span><span class="p">);</span>
<span class="linenos" data-linenos="31 "></span>                    <span class="k">else</span> <span class="n">q</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">v</span><span class="p">);</span><span class="n">num</span><span class="o">++</span><span class="p">;</span><span class="n">x</span><span class="o">+=</span><span class="n">path</span><span class="p">[</span><span class="n">v</span><span class="p">];</span><span class="n">vis</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="nb">true</span><span class="p">;</span>
<span class="linenos" data-linenos="32 "></span>                <span class="p">}</span>
<span class="linenos" data-linenos="33 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos="34 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="35 "></span>    <span class="p">}</span>   
<span class="linenos" data-linenos="36 "></span><span class="p">};</span>
</code></pre></div>
</div></div>
<h2 id="dinic">Dinic</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">maxn</span><span class="o">=</span><span class="mi">4010</span><span class="p">,</span><span class="n">maxe</span><span class="o">=</span><span class="mi">100010</span><span class="o">*</span><span class="mi">2</span><span class="p">;</span>
<span class="linenos" data-linenos=" 2 "></span><span class="k">struct</span> <span class="nc">Graph</span><span class="p">{</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">struct</span> <span class="nc">node</span><span class="p">{</span>
<span class="linenos" data-linenos=" 4 "></span>        <span class="kt">int</span> <span class="n">v</span><span class="p">,</span><span class="n">w</span><span class="p">,</span><span class="n">nxt</span><span class="p">;</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="p">}</span><span class="n">e</span><span class="p">[</span><span class="n">maxe</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">];</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="kt">int</span> <span class="n">head</span><span class="p">[</span><span class="n">maxn</span><span class="p">],</span><span class="n">cur</span><span class="p">[</span><span class="n">maxn</span><span class="p">],</span><span class="n">tot</span><span class="p">;</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="kt">int</span> <span class="n">dis</span><span class="p">[</span><span class="n">maxn</span><span class="p">];</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="kt">int</span> <span class="n">s</span><span class="p">,</span><span class="n">t</span><span class="p">;</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="kt">void</span> <span class="nf">init</span><span class="p">(</span><span class="kt">int</span> <span class="n">_s</span><span class="p">,</span><span class="kt">int</span> <span class="n">_t</span><span class="p">){</span><span class="n">s</span><span class="o">=</span><span class="n">_s</span><span class="p">,</span><span class="n">t</span><span class="o">=</span><span class="n">_t</span><span class="p">;</span><span class="n">tot</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">memset</span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">head</span><span class="p">);}</span>
<span class="linenos" data-linenos="10 "></span>    <span class="n">Graph</span><span class="p">(</span><span class="kt">int</span> <span class="n">_s</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="kt">int</span> <span class="n">_t</span><span class="o">=</span><span class="mi">0</span><span class="p">){</span><span class="n">init</span><span class="p">(</span><span class="n">_s</span><span class="p">,</span><span class="n">_t</span><span class="p">);}</span>
<span class="linenos" data-linenos="11 "></span>    <span class="kt">void</span> <span class="n">add</span><span class="p">(</span><span class="kt">int</span> <span class="n">u</span><span class="p">,</span><span class="kt">int</span> <span class="n">v</span><span class="p">,</span><span class="kt">int</span> <span class="n">w</span><span class="p">){</span>
<span class="linenos" data-linenos="12 "></span>        <span class="c1">//printf(&quot;%d %d %d\n&quot;,u,v,w);</span>
<span class="linenos" data-linenos="13 "></span>        <span class="n">e</span><span class="p">[</span><span class="o">++</span><span class="n">tot</span><span class="p">]</span><span class="o">=</span><span class="p">(</span><span class="n">node</span><span class="p">){</span><span class="n">v</span><span class="p">,</span><span class="n">w</span><span class="p">,</span><span class="n">head</span><span class="p">[</span><span class="n">u</span><span class="p">]},</span><span class="n">head</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">=</span><span class="n">tot</span><span class="p">;</span>
<span class="linenos" data-linenos="14 "></span>        <span class="n">e</span><span class="p">[</span><span class="o">++</span><span class="n">tot</span><span class="p">]</span><span class="o">=</span><span class="p">(</span><span class="n">node</span><span class="p">){</span><span class="n">u</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">head</span><span class="p">[</span><span class="n">v</span><span class="p">]},</span><span class="n">head</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="n">tot</span><span class="p">;</span>
<span class="linenos" data-linenos="15 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="16 "></span>    <span class="cp">#define v e[i].v</span>
<span class="linenos" data-linenos="17 "></span>    <span class="kr">inline</span> <span class="kt">bool</span> <span class="n">bfs</span><span class="p">(){</span>
<span class="linenos" data-linenos="18 "></span>        <span class="n">queue</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">q</span><span class="p">;</span>
<span class="linenos" data-linenos="19 "></span>        <span class="n">memset</span><span class="p">(</span><span class="n">dis</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">dis</span><span class="p">);</span>
<span class="linenos" data-linenos="20 "></span>        <span class="n">memcpy</span><span class="p">(</span><span class="n">cur</span><span class="p">,</span><span class="n">head</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">head</span><span class="p">);</span>
<span class="linenos" data-linenos="21 "></span>        <span class="n">dis</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">q</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>
<span class="linenos" data-linenos="22 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">q</span><span class="p">.</span><span class="n">size</span><span class="p">()){</span>
<span class="linenos" data-linenos="23 "></span>            <span class="kt">int</span> <span class="n">u</span><span class="o">=</span><span class="n">q</span><span class="p">.</span><span class="n">front</span><span class="p">();</span><span class="n">q</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span>
<span class="linenos" data-linenos="24 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="n">head</span><span class="p">[</span><span class="n">u</span><span class="p">];</span><span class="n">i</span><span class="p">;</span><span class="n">i</span><span class="o">=</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">nxt</span><span class="p">)</span>
<span class="linenos" data-linenos="25 "></span>                <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">dis</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">&amp;&amp;</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">w</span><span class="p">){</span>
<span class="linenos" data-linenos="26 "></span>                    <span class="n">dis</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="n">dis</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">q</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">v</span><span class="p">);</span>
<span class="linenos" data-linenos="27 "></span>                    <span class="k">if</span><span class="p">(</span><span class="n">v</span><span class="o">==</span><span class="n">t</span><span class="p">)</span><span class="k">return</span> <span class="nb">true</span><span class="p">;</span>
<span class="linenos" data-linenos="28 "></span>                <span class="p">}</span>
<span class="linenos" data-linenos="29 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="30 "></span>        <span class="k">return</span>  <span class="nb">false</span><span class="p">;</span>
<span class="linenos" data-linenos="31 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="32 "></span>    <span class="kt">int</span> <span class="n">dfs</span><span class="p">(</span><span class="kt">int</span> <span class="n">u</span><span class="p">,</span><span class="kt">int</span> <span class="n">flow</span><span class="p">){</span>
<span class="linenos" data-linenos="33 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">u</span><span class="o">==</span><span class="n">t</span><span class="p">)</span><span class="k">return</span> <span class="n">flow</span><span class="p">;</span>
<span class="linenos" data-linenos="34 "></span>        <span class="kt">int</span> <span class="n">rest</span><span class="o">=</span><span class="n">flow</span><span class="p">;</span>
<span class="linenos" data-linenos="35 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="n">cur</span><span class="p">[</span><span class="n">u</span><span class="p">];</span><span class="n">i</span><span class="o">&amp;&amp;</span><span class="n">rest</span><span class="p">;</span><span class="n">i</span><span class="o">=</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">nxt</span><span class="p">){</span>
<span class="linenos" data-linenos="36 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">dis</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">==</span><span class="n">dis</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">+</span><span class="mi">1</span><span class="o">&amp;&amp;</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">w</span><span class="p">){</span>
<span class="linenos" data-linenos="37 "></span>                <span class="kt">int</span> <span class="n">tmp</span><span class="o">=</span><span class="n">dfs</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="n">min</span><span class="p">(</span><span class="n">rest</span><span class="p">,</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">w</span><span class="p">));</span>
<span class="linenos" data-linenos="38 "></span>                <span class="n">rest</span><span class="o">-=</span><span class="n">tmp</span><span class="p">,</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">w</span><span class="o">-=</span><span class="n">tmp</span><span class="p">,</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="o">^</span><span class="mi">1</span><span class="p">].</span><span class="n">w</span><span class="o">+=</span><span class="n">tmp</span><span class="p">;</span>
<span class="linenos" data-linenos="39 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos="40 "></span>            <span class="n">cur</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">=</span><span class="n">i</span><span class="p">;</span>
<span class="linenos" data-linenos="41 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="42 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">rest</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span><span class="n">dis</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">=</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="43 "></span>        <span class="k">return</span> <span class="n">flow</span><span class="o">-</span><span class="n">rest</span><span class="p">;</span>
<span class="linenos" data-linenos="44 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="45 "></span>    <span class="cp">#undef v</span>
<span class="linenos" data-linenos="46 "></span>    <span class="kt">int</span> <span class="n">dinic</span><span class="p">(){</span>
<span class="linenos" data-linenos="47 "></span>        <span class="kt">int</span> <span class="n">ans</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="48 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">bfs</span><span class="p">())</span>
<span class="linenos" data-linenos="49 "></span>            <span class="k">while</span><span class="p">(</span><span class="kt">int</span> <span class="n">sth</span><span class="o">=</span><span class="n">dfs</span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="mf">2e9</span><span class="p">))</span>
<span class="linenos" data-linenos="50 "></span>                <span class="n">ans</span><span class="o">+=</span><span class="n">sth</span><span class="p">;</span>
<span class="linenos" data-linenos="51 "></span>        <span class="k">return</span> <span class="n">ans</span><span class="p">;</span>
<span class="linenos" data-linenos="52 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="53 "></span><span class="p">}</span><span class="n">G</span><span class="p">;</span> 
</code></pre></div>
</div></div>
<h2 id="mincostmaxflow">MincostMaxflow</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">maxn</span><span class="o">=</span><span class="mi">100005</span><span class="p">;</span>
<span class="linenos" data-linenos=" 2 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">maxe</span><span class="o">=</span><span class="mi">100005</span><span class="p">;</span>
<span class="linenos" data-linenos=" 3 "></span><span class="k">struct</span> <span class="nc">Graph</span><span class="p">{</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="kt">int</span> <span class="n">s</span><span class="p">,</span><span class="n">t</span><span class="p">;</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="kt">int</span> <span class="n">dis</span><span class="p">[</span><span class="n">maxn</span><span class="p">],</span><span class="n">pre</span><span class="p">[</span><span class="n">maxn</span><span class="p">],</span><span class="n">lste</span><span class="p">[</span><span class="n">maxn</span><span class="p">],</span><span class="n">flow</span><span class="p">[</span><span class="n">maxn</span><span class="p">];</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="kt">bool</span> <span class="n">inq</span><span class="p">[</span><span class="n">maxn</span><span class="p">];</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="k">struct</span> <span class="nc">node</span><span class="p">{</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="kt">int</span> <span class="n">v</span><span class="p">,</span><span class="n">w</span><span class="p">,</span><span class="n">cost</span><span class="p">,</span><span class="n">nxt</span><span class="p">;</span>
<span class="linenos" data-linenos=" 9 "></span>        <span class="n">node</span><span class="p">(</span><span class="kt">int</span> <span class="n">_v</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="kt">int</span> <span class="n">_w</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="kt">int</span> <span class="n">_c</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="kt">int</span> <span class="n">_n</span><span class="o">=</span><span class="mi">0</span><span class="p">){</span><span class="n">v</span><span class="o">=</span><span class="n">_v</span><span class="p">,</span><span class="n">w</span><span class="o">=</span><span class="n">_w</span><span class="p">,</span><span class="n">cost</span><span class="o">=</span><span class="n">_c</span><span class="p">,</span><span class="n">nxt</span><span class="o">=</span><span class="n">_n</span><span class="p">;}</span>
<span class="linenos" data-linenos="10 "></span>    <span class="p">}</span><span class="n">e</span><span class="p">[</span><span class="n">maxe</span><span class="p">];</span>
<span class="linenos" data-linenos="11 "></span>    <span class="kt">int</span> <span class="n">tot</span><span class="p">,</span><span class="n">head</span><span class="p">[</span><span class="n">maxn</span><span class="p">];</span>
<span class="linenos" data-linenos="12 "></span>    <span class="kt">void</span> <span class="nf">init</span><span class="p">(</span><span class="kt">int</span> <span class="n">_s</span><span class="p">,</span><span class="kt">int</span> <span class="n">_t</span><span class="p">){</span>
<span class="linenos" data-linenos="13 "></span>        <span class="n">memset</span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">head</span><span class="p">);</span>
<span class="linenos" data-linenos="14 "></span>        <span class="n">tot</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">s</span><span class="o">=</span><span class="n">_s</span><span class="p">,</span><span class="n">t</span><span class="o">=</span><span class="n">_t</span><span class="p">;</span>
<span class="linenos" data-linenos="15 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="16 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">add</span><span class="p">(</span><span class="kt">int</span> <span class="n">u</span><span class="p">,</span><span class="kt">int</span> <span class="n">v</span><span class="p">,</span><span class="kt">int</span> <span class="n">w</span><span class="p">,</span><span class="kt">int</span> <span class="n">c</span><span class="p">){</span>
<span class="linenos" data-linenos="17 "></span>        <span class="n">e</span><span class="p">[</span><span class="o">++</span><span class="n">tot</span><span class="p">]</span><span class="o">=</span><span class="n">node</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="n">w</span><span class="p">,</span><span class="n">c</span><span class="p">,</span><span class="n">head</span><span class="p">[</span><span class="n">u</span><span class="p">]);</span><span class="n">head</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">=</span><span class="n">tot</span><span class="p">;</span>
<span class="linenos" data-linenos="18 "></span>        <span class="n">e</span><span class="p">[</span><span class="o">++</span><span class="n">tot</span><span class="p">]</span><span class="o">=</span><span class="n">node</span><span class="p">(</span><span class="n">u</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="o">-</span><span class="n">c</span><span class="p">,</span><span class="n">head</span><span class="p">[</span><span class="n">v</span><span class="p">]);</span><span class="n">head</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="n">tot</span><span class="p">;</span>
<span class="linenos" data-linenos="19 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="20 "></span>    <span class="kt">bool</span> <span class="n">spfa</span><span class="p">(){</span>
<span class="linenos" data-linenos="21 "></span>        <span class="n">queue</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">q</span><span class="p">;</span>
<span class="linenos" data-linenos="22 "></span>        <span class="n">memset</span><span class="p">(</span><span class="n">dis</span><span class="p">,</span><span class="mi">63</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">dis</span><span class="p">);</span>
<span class="linenos" data-linenos="23 "></span>        <span class="n">memset</span><span class="p">(</span><span class="n">flow</span><span class="p">,</span><span class="mi">63</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">flow</span><span class="p">);</span>
<span class="linenos" data-linenos="24 "></span>        <span class="n">memset</span><span class="p">(</span><span class="n">inq</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">inq</span><span class="p">);</span>
<span class="linenos" data-linenos="25 "></span>        <span class="n">dis</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">pre</span><span class="p">[</span><span class="n">t</span><span class="p">]</span><span class="o">=</span><span class="mi">-1</span><span class="p">;</span><span class="n">q</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>
<span class="linenos" data-linenos="26 "></span>        <span class="k">while</span><span class="p">(</span><span class="o">!</span><span class="n">q</span><span class="p">.</span><span class="n">empty</span><span class="p">()){</span>
<span class="linenos" data-linenos="27 "></span>            <span class="kt">int</span> <span class="n">u</span><span class="o">=</span><span class="n">q</span><span class="p">.</span><span class="n">front</span><span class="p">();</span><span class="n">q</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span><span class="n">inq</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="28 "></span>            <span class="cp">#define v e[i].v</span>
<span class="linenos" data-linenos="29 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="n">head</span><span class="p">[</span><span class="n">u</span><span class="p">];</span><span class="n">i</span><span class="p">;</span><span class="n">i</span><span class="o">=</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">nxt</span><span class="p">){</span>
<span class="linenos" data-linenos="30 "></span>                <span class="k">if</span><span class="p">(</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">w</span><span class="o">&gt;</span><span class="mi">0</span><span class="o">&amp;&amp;</span><span class="n">dis</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">&gt;</span><span class="n">dis</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">+</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">cost</span><span class="p">){</span>
<span class="linenos" data-linenos="31 "></span>                    <span class="n">dis</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="n">dis</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">+</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">cost</span><span class="p">;</span>
<span class="linenos" data-linenos="32 "></span>                    <span class="n">pre</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="n">u</span><span class="p">,</span><span class="n">lste</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="n">i</span><span class="p">;</span><span class="c1">//lste 表示顶点v是由哪条边过来的</span>
<span class="linenos" data-linenos="33 "></span>                    <span class="n">flow</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="n">min</span><span class="p">(</span><span class="n">flow</span><span class="p">[</span><span class="n">u</span><span class="p">],</span><span class="n">e</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">w</span><span class="p">);</span>
<span class="linenos" data-linenos="34 "></span>                    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">inq</span><span class="p">[</span><span class="n">v</span><span class="p">])</span><span class="n">inq</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">q</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">v</span><span class="p">);</span>
<span class="linenos" data-linenos="35 "></span>                <span class="p">}</span>
<span class="linenos" data-linenos="36 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos="37 "></span>            <span class="cp">#undef v</span>
<span class="linenos" data-linenos="38 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="39 "></span>        <span class="k">return</span> <span class="n">pre</span><span class="p">[</span><span class="n">t</span><span class="p">]</span><span class="o">!=</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="40 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="41 "></span>    <span class="n">pair</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">mcmf</span><span class="p">(){</span>
<span class="linenos" data-linenos="42 "></span>        <span class="kt">int</span> <span class="n">mincost</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">maxflow</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="43 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">spfa</span><span class="p">()){</span>
<span class="linenos" data-linenos="44 "></span>            <span class="kt">int</span> <span class="n">u</span><span class="o">=</span><span class="n">t</span><span class="p">;</span>
<span class="linenos" data-linenos="45 "></span>            <span class="n">maxflow</span><span class="o">+=</span><span class="n">flow</span><span class="p">[</span><span class="n">t</span><span class="p">],</span><span class="n">mincost</span><span class="o">+=</span><span class="n">flow</span><span class="p">[</span><span class="n">t</span><span class="p">]</span><span class="o">*</span><span class="n">dis</span><span class="p">[</span><span class="n">t</span><span class="p">];</span>
<span class="linenos" data-linenos="46 "></span>            <span class="k">while</span><span class="p">(</span><span class="n">u</span><span class="o">!=</span><span class="n">s</span><span class="p">){</span>
<span class="linenos" data-linenos="47 "></span>                <span class="kt">int</span> <span class="n">E</span><span class="o">=</span><span class="n">lste</span><span class="p">[</span><span class="n">u</span><span class="p">];</span>
<span class="linenos" data-linenos="48 "></span>                <span class="n">e</span><span class="p">[</span><span class="n">E</span><span class="p">].</span><span class="n">w</span><span class="o">-=</span><span class="n">flow</span><span class="p">[</span><span class="n">t</span><span class="p">],</span><span class="n">e</span><span class="p">[</span><span class="n">E</span><span class="o">^</span><span class="mi">1</span><span class="p">].</span><span class="n">w</span><span class="o">+=</span><span class="n">flow</span><span class="p">[</span><span class="n">t</span><span class="p">];</span>
<span class="linenos" data-linenos="49 "></span>                <span class="n">u</span><span class="o">=</span><span class="n">pre</span><span class="p">[</span><span class="n">u</span><span class="p">];</span>
<span class="linenos" data-linenos="50 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos="51 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="52 "></span>        <span class="k">return</span> <span class="n">make_pair</span><span class="p">(</span><span class="n">mincost</span><span class="p">,</span><span class="n">maxflow</span><span class="p">);</span>
<span class="linenos" data-linenos="53 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="54 "></span><span class="p">}</span><span class="n">G</span><span class="p">;</span>
</code></pre></div>
</div></div>
<h1 id="_3">数学</h1>
<h2 id="_4">自适应辛普森法</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">struct</span> <span class="nc">Simpson</span><span class="p">{</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="kt">double</span> <span class="p">(</span><span class="o">*</span><span class="n">f</span><span class="p">)(</span><span class="kt">double</span><span class="p">);</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="kt">double</span> <span class="n">Eps</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="n">Simpson</span><span class="p">(</span><span class="kt">double</span> <span class="p">(</span><span class="o">*</span><span class="n">_f</span><span class="p">)(</span><span class="kt">double</span><span class="p">),</span><span class="kt">double</span> <span class="n">_E</span><span class="p">){</span><span class="n">f</span><span class="o">=</span><span class="n">_f</span><span class="p">;</span><span class="n">Eps</span><span class="o">=</span><span class="n">_E</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="kr">inline</span> <span class="kt">double</span> <span class="n">simpson</span><span class="p">(</span><span class="kt">double</span> <span class="n">l</span><span class="p">,</span><span class="kt">double</span> <span class="n">r</span><span class="p">){</span>
<span class="linenos" data-linenos=" 6 "></span>        <span class="kt">double</span> <span class="n">mid</span><span class="o">=</span><span class="p">(</span><span class="n">l</span><span class="o">+</span><span class="n">r</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">;</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="k">return</span> <span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">l</span><span class="p">)</span><span class="o">+</span><span class="mi">4</span><span class="o">*</span><span class="n">f</span><span class="p">(</span><span class="n">mid</span><span class="p">)</span><span class="o">+</span><span class="n">f</span><span class="p">(</span><span class="n">r</span><span class="p">))</span><span class="o">*</span><span class="p">(</span><span class="n">r</span><span class="o">-</span><span class="n">l</span><span class="p">)</span><span class="o">/</span><span class="mi">6</span><span class="p">;</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="kt">double</span> <span class="n">asr</span><span class="p">(</span><span class="kt">double</span> <span class="n">l</span><span class="p">,</span><span class="kt">double</span> <span class="n">r</span><span class="p">,</span><span class="kt">double</span> <span class="n">eps</span><span class="p">,</span><span class="kt">double</span> <span class="n">ans</span><span class="p">){</span>
<span class="linenos" data-linenos="10 "></span>        <span class="kt">double</span> <span class="n">mid</span><span class="o">=</span><span class="p">(</span><span class="n">l</span><span class="o">+</span><span class="n">r</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">;</span>
<span class="linenos" data-linenos="11 "></span>        <span class="kt">double</span> <span class="n">l_</span><span class="o">=</span><span class="n">simpson</span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="n">mid</span><span class="p">),</span><span class="n">r_</span><span class="o">=</span><span class="n">simpson</span><span class="p">(</span><span class="n">mid</span><span class="p">,</span><span class="n">r</span><span class="p">);</span>
<span class="linenos" data-linenos="12 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">fabs</span><span class="p">(</span><span class="n">l_</span><span class="o">+</span><span class="n">r_</span><span class="o">-</span><span class="n">ans</span><span class="p">)</span><span class="o">&lt;=</span><span class="mi">15</span><span class="o">*</span><span class="n">eps</span><span class="p">)</span><span class="k">return</span> <span class="n">l_</span><span class="o">+</span><span class="n">r_</span><span class="o">+</span><span class="p">(</span><span class="n">l_</span><span class="o">+</span><span class="n">r_</span><span class="o">-</span><span class="n">ans</span><span class="p">)</span><span class="o">/</span><span class="mi">15</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>        <span class="k">return</span> <span class="nf">asr</span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="n">mid</span><span class="p">,</span><span class="n">eps</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span><span class="n">l_</span><span class="p">)</span><span class="o">+</span><span class="n">asr</span><span class="p">(</span><span class="n">mid</span><span class="p">,</span><span class="n">r</span><span class="p">,</span><span class="n">eps</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span><span class="n">r_</span><span class="p">);</span>
<span class="linenos" data-linenos="14 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="15 "></span>    <span class="kt">double</span> <span class="n">asr</span><span class="p">(</span><span class="kt">double</span> <span class="n">l</span><span class="p">,</span><span class="kt">double</span> <span class="n">r</span><span class="p">){</span>
<span class="linenos" data-linenos="16 "></span>        <span class="k">return</span> <span class="nf">asr</span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="n">r</span><span class="p">,</span><span class="n">Eps</span><span class="p">,</span><span class="n">simpson</span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="n">r</span><span class="p">));</span>
<span class="linenos" data-linenos="17 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="18 "></span><span class="p">};</span>
</code></pre></div>
</div></div>
<h2 id="_5">凸包</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">namespace</span> <span class="n">Convex_Hull</span><span class="p">{</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="cp">#define sq(x) (x)*(x)</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">struct</span> <span class="nc">node</span><span class="p">{</span>
<span class="linenos" data-linenos=" 4 "></span>        <span class="kt">double</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">;</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="p">}</span><span class="n">p</span><span class="p">[</span><span class="mi">1000000</span><span class="o">+</span><span class="mi">1000</span><span class="p">],</span><span class="n">s</span><span class="p">[</span><span class="mi">1000000</span><span class="o">+</span><span class="mi">1000</span><span class="p">];</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">cnt</span><span class="p">;</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="kt">double</span> <span class="nf">check</span><span class="p">(</span><span class="n">node</span> <span class="n">a1</span><span class="p">,</span><span class="n">node</span> <span class="n">a2</span><span class="p">,</span><span class="n">node</span> <span class="n">b1</span><span class="p">,</span><span class="n">node</span> <span class="n">b2</span><span class="p">){</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="k">return</span> <span class="p">(</span><span class="n">a2</span><span class="p">.</span><span class="n">x</span><span class="o">-</span><span class="n">a1</span><span class="p">.</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">b2</span><span class="p">.</span><span class="n">y</span><span class="o">-</span><span class="n">b1</span><span class="p">.</span><span class="n">y</span><span class="p">)</span><span class="o">-</span><span class="p">(</span><span class="n">a2</span><span class="p">.</span><span class="n">y</span><span class="o">-</span><span class="n">a1</span><span class="p">.</span><span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">b2</span><span class="p">.</span><span class="n">x</span><span class="o">-</span><span class="n">b1</span><span class="p">.</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="p">}</span><span class="kt">double</span> <span class="nf">d</span><span class="p">(</span><span class="n">node</span> <span class="n">a</span><span class="p">,</span><span class="n">node</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="10 "></span>        <span class="k">return</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">sq</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="o">-</span><span class="n">b</span><span class="p">.</span><span class="n">x</span><span class="p">)</span><span class="o">+</span><span class="n">sq</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">y</span><span class="o">-</span><span class="n">b</span><span class="p">.</span><span class="n">y</span><span class="p">));</span>
<span class="linenos" data-linenos="11 "></span>    <span class="p">}</span><span class="kt">bool</span> <span class="nf">cmp</span><span class="p">(</span><span class="n">node</span> <span class="n">p1</span><span class="p">,</span><span class="n">node</span> <span class="n">p2</span><span class="p">){</span>
<span class="linenos" data-linenos="12 "></span>        <span class="kt">double</span> <span class="n">tmp</span><span class="o">=</span><span class="n">check</span><span class="p">(</span><span class="n">p1</span><span class="p">,</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="n">p2</span><span class="p">,</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
<span class="linenos" data-linenos="13 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">tmp</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">)</span><span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="14 "></span>        <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">tmp</span><span class="o">==</span><span class="mi">0</span><span class="o">&amp;&amp;</span><span class="n">d</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">p1</span><span class="p">)</span><span class="o">&lt;</span><span class="n">d</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">p2</span><span class="p">))</span><span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="15 "></span>        <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="16 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="17 "></span>    <span class="kt">void</span> <span class="nf">add</span><span class="p">(</span><span class="kt">double</span> <span class="n">x</span><span class="p">,</span><span class="kt">double</span> <span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos="18 "></span>        <span class="n">n</span><span class="o">++</span><span class="p">;</span>
<span class="linenos" data-linenos="19 "></span>        <span class="n">p</span><span class="p">[</span><span class="n">n</span><span class="p">].</span><span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="p">,</span><span class="n">p</span><span class="p">[</span><span class="n">n</span><span class="p">].</span><span class="n">y</span><span class="o">=</span><span class="n">y</span><span class="p">;</span>
<span class="linenos" data-linenos="20 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="n">n</span><span class="p">].</span><span class="n">y</span><span class="o">&lt;</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="n">y</span><span class="p">)</span><span class="n">swap</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="n">n</span><span class="p">],</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
<span class="linenos" data-linenos="21 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="22 "></span>    <span class="kt">void</span> <span class="nf">deal</span><span class="p">(){</span>
<span class="linenos" data-linenos="23 "></span>        <span class="n">sort</span><span class="p">(</span><span class="n">p</span><span class="o">+</span><span class="mi">2</span><span class="p">,</span><span class="n">p</span><span class="o">+</span><span class="mi">1</span><span class="o">+</span><span class="n">n</span><span class="p">,</span><span class="n">cmp</span><span class="p">);</span>
<span class="linenos" data-linenos="24 "></span>        <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span><span class="n">cnt</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="25 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;=</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="linenos" data-linenos="26 "></span>            <span class="k">while</span><span class="p">(</span><span class="n">cnt</span><span class="o">&gt;</span><span class="mi">1</span><span class="o">&amp;&amp;</span><span class="n">check</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="n">cnt</span><span class="mi">-1</span><span class="p">],</span><span class="n">s</span><span class="p">[</span><span class="n">cnt</span><span class="p">],</span><span class="n">s</span><span class="p">[</span><span class="n">cnt</span><span class="p">],</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="o">&lt;=</span><span class="mi">0</span><span class="p">)</span>
<span class="linenos" data-linenos="27 "></span>                <span class="n">cnt</span><span class="o">--</span><span class="p">;</span>
<span class="linenos" data-linenos="28 "></span>            <span class="n">s</span><span class="p">[</span><span class="o">++</span><span class="n">cnt</span><span class="p">]</span><span class="o">=</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="29 "></span>        <span class="p">}</span><span class="n">s</span><span class="p">[</span><span class="n">cnt</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
<span class="linenos" data-linenos="30 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="31 "></span>    <span class="kt">double</span> <span class="nf">C</span><span class="p">(){</span>
<span class="linenos" data-linenos="32 "></span>        <span class="kt">double</span> <span class="n">ans</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="33 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;=</span><span class="n">cnt</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos="34 "></span>            <span class="n">ans</span><span class="o">+=</span><span class="n">d</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]);</span>
<span class="linenos" data-linenos="35 "></span>        <span class="k">return</span> <span class="n">ans</span><span class="p">;</span>
<span class="linenos" data-linenos="36 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="37 "></span><span class="p">};</span>
</code></pre></div>
</div></div>
<h2 id="_6">高斯消元</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="p">,</span><span class="k">const</span> <span class="kt">int</span> <span class="n">M</span><span class="o">&gt;</span>
<span class="linenos" data-linenos=" 2 "></span><span class="k">struct</span> <span class="nc">Gauss</span><span class="p">{</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="c1">//解n元方程组</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="p">;</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="n">T</span> <span class="n">B</span><span class="p">[</span><span class="n">M</span><span class="p">][</span><span class="n">M</span><span class="o">+</span><span class="mi">1</span><span class="p">];</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="n">T</span> <span class="n">ans</span><span class="p">[</span><span class="n">M</span><span class="p">];</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="kt">bool</span> <span class="nf">run</span><span class="p">(){</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="linenos" data-linenos=" 9 "></span>            <span class="kt">int</span> <span class="n">pivot</span><span class="o">=</span><span class="n">i</span><span class="p">;</span>
<span class="linenos" data-linenos="10 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">j</span><span class="o">=</span><span class="n">i</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos="11 "></span>                <span class="k">if</span><span class="p">(</span><span class="n">B</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">i</span><span class="p">]</span><span class="o">==</span><span class="n">B</span><span class="p">[</span><span class="n">pivot</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
<span class="linenos" data-linenos="12 "></span>                    <span class="n">pivot</span><span class="o">=</span><span class="n">j</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;=</span><span class="n">n</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="n">swap</span><span class="p">(</span><span class="n">B</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">],</span><span class="n">B</span><span class="p">[</span><span class="n">pivot</span><span class="p">][</span><span class="n">j</span><span class="p">]);</span>
<span class="linenos" data-linenos="14 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">B</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">i</span><span class="p">]</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span><span class="k">return</span> <span class="nb">false</span><span class="p">;</span>
<span class="linenos" data-linenos="15 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">j</span><span class="o">=</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;=</span><span class="n">n</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="n">B</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="o">/=</span><span class="n">B</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="16 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos="17 "></span>                <span class="k">if</span><span class="p">(</span><span class="n">i</span><span class="o">!=</span><span class="n">j</span><span class="p">)</span>
<span class="linenos" data-linenos="18 "></span>                    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span><span class="n">k</span><span class="o">&lt;=</span><span class="n">n</span><span class="p">;</span><span class="n">k</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos="19 "></span>                        <span class="n">B</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">]</span><span class="o">-=</span><span class="n">B</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">B</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">k</span><span class="p">];</span>
<span class="linenos" data-linenos="20 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="21 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">ans</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">B</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">n</span><span class="p">];</span>
<span class="linenos" data-linenos="22 "></span>        <span class="k">return</span> <span class="nb">true</span><span class="p">;</span>
<span class="linenos" data-linenos="23 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="24 "></span><span class="p">};</span>
<span class="linenos" data-linenos="25 "></span><span class="k">const</span> <span class="kt">double</span> <span class="n">EPS</span><span class="o">=</span><span class="mf">1e-8</span><span class="p">;</span>
<span class="linenos" data-linenos="26 "></span><span class="k">struct</span> <span class="nc">node</span><span class="p">{</span>
<span class="linenos" data-linenos="27 "></span>    <span class="kt">double</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="28 "></span>    <span class="n">node</span><span class="p">(</span><span class="kt">double</span> <span class="n">_</span><span class="o">=</span><span class="mi">0</span><span class="p">){</span><span class="n">x</span><span class="o">=</span><span class="n">_</span><span class="p">;}</span>
<span class="linenos" data-linenos="29 "></span>    <span class="kt">bool</span> <span class="k">operator</span><span class="o">==</span><span class="p">(</span><span class="n">node</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="nf">fabs</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">b</span><span class="p">.</span><span class="n">x</span><span class="p">)</span><span class="o">&lt;=</span><span class="n">EPS</span><span class="p">;}</span>
<span class="linenos" data-linenos="30 "></span>    <span class="kt">bool</span> <span class="k">operator</span><span class="o">==</span><span class="p">(</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="nf">fabs</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">b</span><span class="p">)</span><span class="o">&lt;=</span><span class="n">EPS</span><span class="p">;}</span>
<span class="linenos" data-linenos="31 "></span>    <span class="n">node</span> <span class="k">operator</span><span class="o">*</span><span class="p">(</span><span class="n">node</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="nf">node</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">x</span><span class="p">);}</span>
<span class="linenos" data-linenos="32 "></span>    <span class="n">node</span> <span class="o">*</span><span class="k">operator</span><span class="o">/=</span><span class="p">(</span><span class="n">node</span> <span class="n">b</span><span class="p">){</span><span class="n">x</span><span class="o">/=</span><span class="n">b</span><span class="p">.</span><span class="n">x</span><span class="p">;</span><span class="k">return</span> <span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="33 "></span>    <span class="n">node</span> <span class="o">*</span><span class="k">operator</span><span class="o">-=</span><span class="p">(</span><span class="n">node</span> <span class="n">b</span><span class="p">){</span><span class="n">x</span><span class="o">-=</span><span class="n">b</span><span class="p">.</span><span class="n">x</span><span class="p">;</span><span class="k">return</span> <span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="34 "></span><span class="p">};</span>
<span class="linenos" data-linenos="35 "></span><span class="n">Gauss</span><span class="o">&lt;</span><span class="n">node</span><span class="p">,</span><span class="mi">120</span><span class="o">&gt;</span><span class="n">G</span><span class="p">;</span>
</code></pre></div>
</div></div>
<h1 id="_7">数论</h1>
<h2 id="_8">起手式</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">namespace</span> <span class="n">Math</span><span class="p">{</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="k">const</span> <span class="kt">int</span> <span class="n">N</span><span class="o">=</span><span class="mi">100000</span><span class="o">+</span><span class="mi">50</span><span class="p">;</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">typedef</span> <span class="kt">int</span> <span class="n">ll</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="n">ll</span> <span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="k">struct</span> <span class="nc">modint</span><span class="p">{</span>
<span class="linenos" data-linenos=" 6 "></span>        <span class="n">ll</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="n">modint</span><span class="p">(</span><span class="n">ll</span> <span class="n">o</span><span class="o">=</span><span class="mi">0</span><span class="p">){</span><span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">=</span> <span class="p">(</span><span class="n">ll</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 9 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="n">modint</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">&gt;=</span><span class="n">mod</span><span class="o">?</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">-</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="10 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">-=</span><span class="p">(</span><span class="n">modint</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">+</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="11 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="n">modint</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">%</span><span class="n">mod</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="12 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">^=</span><span class="p">(</span><span class="n">ll</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="13 "></span>            <span class="n">b</span><span class="o">%=</span><span class="p">(</span><span class="n">mod</span><span class="mi">-1</span><span class="p">);</span>
<span class="linenos" data-linenos="14 "></span>            <span class="n">modint</span> <span class="n">a</span><span class="o">=*</span><span class="k">this</span><span class="p">,</span><span class="n">c</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="15 "></span>            <span class="k">for</span><span class="p">(;</span><span class="n">b</span><span class="p">;</span><span class="n">b</span><span class="o">&gt;&gt;=</span><span class="mi">1</span><span class="p">,</span><span class="n">a</span><span class="o">*=</span><span class="n">a</span><span class="p">)</span><span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="n">c</span><span class="o">*=</span><span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="16 "></span>            <span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">c</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;</span>
<span class="linenos" data-linenos="17 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="18 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">/=</span><span class="p">(</span><span class="n">modint</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span> <span class="o">*=</span><span class="n">o</span><span class="o">^=</span><span class="n">mod</span><span class="mi">-2</span><span class="p">;}</span>
<span class="linenos" data-linenos="19 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="n">ll</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="o">&gt;=</span><span class="n">mod</span><span class="o">?</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="o">-</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="20 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">-=</span><span class="p">(</span><span class="n">ll</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="o">+</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="21 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="n">ll</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">o</span><span class="o">%</span><span class="n">mod</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="22 "></span>        <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">/=</span><span class="p">(</span><span class="n">ll</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span> <span class="o">*=</span> <span class="p">((</span><span class="n">modint</span><span class="p">(</span><span class="n">o</span><span class="p">))</span><span class="o">^=</span><span class="n">mod</span><span class="mi">-2</span><span class="p">);}</span>
<span class="linenos" data-linenos="23 "></span>        <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">+</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">+=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="24 "></span>        <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">-</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">-=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="25 "></span>        <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">*</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">*=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="26 "></span>        <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">/</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">/=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="27 "></span>        <span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">^</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">ll</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">^=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="28 "></span>        <span class="k">friend</span> <span class="kt">bool</span> <span class="k">operator</span> <span class="o">==</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">ll</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="o">==</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="29 "></span>        <span class="k">friend</span> <span class="kt">bool</span> <span class="k">operator</span> <span class="o">!=</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">ll</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="o">!=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="30 "></span>        <span class="kt">bool</span> <span class="k">operator</span> <span class="o">!</span> <span class="p">()</span> <span class="p">{</span><span class="k">return</span> <span class="o">!</span><span class="n">x</span><span class="p">;}</span>
<span class="linenos" data-linenos="31 "></span>        <span class="n">modint</span> <span class="k">operator</span> <span class="o">-</span> <span class="p">()</span> <span class="p">{</span><span class="k">return</span> <span class="n">x</span><span class="o">?</span><span class="n">mod</span><span class="o">-</span><span class="nl">x</span><span class="p">:</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos="32 "></span>    <span class="p">};</span>
<span class="linenos" data-linenos="33 "></span>    <span class="kt">int</span> <span class="n">prime</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">tot</span><span class="p">,</span><span class="n">phi</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">miu</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">in</span><span class="p">[</span><span class="n">N</span><span class="p">];</span>
<span class="linenos" data-linenos="34 "></span>    <span class="kt">bool</span> <span class="n">isprime</span><span class="p">[</span><span class="n">N</span><span class="p">];</span>
<span class="linenos" data-linenos="35 "></span>    <span class="n">modint</span> <span class="n">fac</span><span class="p">[</span><span class="n">N</span><span class="p">];</span>
<span class="linenos" data-linenos="36 "></span>    <span class="n">modint</span> <span class="n">f</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">sum</span><span class="p">[</span><span class="n">N</span><span class="p">];</span>
<span class="linenos" data-linenos="37 "></span>    <span class="kt">void</span> <span class="nf">init</span><span class="p">(){</span>
<span class="linenos" data-linenos="38 "></span>        <span class="n">memset</span><span class="p">(</span><span class="n">isprime</span><span class="p">,</span><span class="mh">0xff</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">isprime</span><span class="p">);</span>
<span class="linenos" data-linenos="39 "></span>        <span class="n">tot</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">phi</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="n">miu</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="40 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">N</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="linenos" data-linenos="41 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">isprime</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<span class="linenos" data-linenos="42 "></span>                <span class="n">prime</span><span class="p">[</span><span class="o">++</span><span class="n">tot</span><span class="p">]</span><span class="o">=</span><span class="n">i</span><span class="p">,</span><span class="n">miu</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="mi">-1</span><span class="p">,</span><span class="n">phi</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">i</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="43 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">j</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;=</span><span class="n">tot</span><span class="o">&amp;&amp;</span><span class="n">i</span><span class="o">*</span><span class="n">prime</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">&lt;</span><span class="n">N</span><span class="p">;</span><span class="n">j</span><span class="o">++</span><span class="p">){</span>
<span class="linenos" data-linenos="44 "></span>                <span class="n">isprime</span><span class="p">[</span><span class="n">i</span><span class="o">*</span><span class="n">prime</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span><span class="o">=</span><span class="nb">false</span><span class="p">;</span>
<span class="linenos" data-linenos="45 "></span>                <span class="k">if</span><span class="p">(</span><span class="n">i</span><span class="o">%</span><span class="n">prime</span><span class="p">[</span><span class="n">j</span><span class="p">]){</span>
<span class="linenos" data-linenos="46 "></span>                    <span class="n">miu</span><span class="p">[</span><span class="n">i</span><span class="o">*</span><span class="n">prime</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span><span class="o">=-</span><span class="n">miu</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="47 "></span>                    <span class="n">phi</span><span class="p">[</span><span class="n">i</span><span class="o">*</span><span class="n">prime</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span><span class="o">=</span><span class="n">phi</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">phi</span><span class="p">[</span><span class="n">prime</span><span class="p">[</span><span class="n">j</span><span class="p">]];</span>
<span class="linenos" data-linenos="48 "></span>                <span class="p">}</span>
<span class="linenos" data-linenos="49 "></span>                <span class="k">else</span><span class="p">{</span>
<span class="linenos" data-linenos="50 "></span>                    <span class="n">miu</span><span class="p">[</span><span class="n">i</span><span class="o">*</span><span class="n">prime</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="51 "></span>                    <span class="n">phi</span><span class="p">[</span><span class="n">i</span><span class="o">*</span><span class="n">prime</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span><span class="o">=</span><span class="n">phi</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">prime</span><span class="p">[</span><span class="n">j</span><span class="p">];</span>
<span class="linenos" data-linenos="52 "></span>                <span class="p">}</span>
<span class="linenos" data-linenos="53 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos="54 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="55 "></span>        <span class="n">in</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">N</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">in</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="p">(</span><span class="n">mod</span><span class="o">-</span><span class="n">mod</span><span class="o">/</span><span class="n">i</span><span class="p">)</span><span class="o">*</span><span class="n">in</span><span class="p">[</span><span class="n">mod</span><span class="o">%</span><span class="n">i</span><span class="p">]</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos="56 "></span>        <span class="n">fac</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">N</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">fac</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">fac</span><span class="p">[</span><span class="n">i</span><span class="mi">-1</span><span class="p">]</span><span class="o">*</span><span class="n">i</span><span class="p">;</span>
<span class="linenos" data-linenos="57 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="58 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
<h2 id="_9">快速幂</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="1 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span><span class="n">T</span> <span class="n">ksm</span><span class="p">(</span><span class="n">T</span> <span class="n">a</span><span class="p">,</span><span class="n">T</span> <span class="n">b</span><span class="p">,</span><span class="n">T</span> <span class="n">c</span><span class="p">){</span>
<span class="linenos" data-linenos="2 "></span>    <span class="n">T</span> <span class="n">res</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="3 "></span>    <span class="k">while</span><span class="p">(</span><span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="4 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="n">res</span><span class="o">=</span><span class="n">res</span><span class="o">*</span><span class="n">a</span><span class="o">%</span><span class="n">c</span><span class="p">;</span>
<span class="linenos" data-linenos="5 "></span>        <span class="n">a</span><span class="o">=</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">%</span><span class="n">c</span><span class="p">;</span><span class="n">b</span><span class="o">&gt;&gt;=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="6 "></span>    <span class="p">}</span><span class="k">return</span> <span class="n">res</span><span class="p">;</span>
<span class="linenos" data-linenos="7 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
<h2 id="_10">逆元</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span><span class="kt">void</span> <span class="n">exgcd</span><span class="p">(</span><span class="n">T</span> <span class="n">a</span><span class="p">,</span><span class="n">T</span> <span class="n">b</span><span class="p">,</span><span class="n">T</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">,</span><span class="n">T</span> <span class="o">&amp;</span><span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">b</span><span class="p">)</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">else</span><span class="p">{</span>
<span class="linenos" data-linenos=" 4 "></span>        <span class="n">exgcd</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">a</span><span class="o">%</span><span class="n">b</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos=" 5 "></span>        <span class="n">T</span> <span class="n">t</span><span class="o">=</span><span class="n">x</span><span class="p">;</span><span class="n">x</span><span class="o">=</span><span class="n">y</span><span class="p">;</span>
<span class="linenos" data-linenos=" 6 "></span>        <span class="n">y</span><span class="o">=</span><span class="n">t</span><span class="o">-</span><span class="n">a</span><span class="o">/</span><span class="n">b</span><span class="o">*</span><span class="n">y</span><span class="p">;</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 8 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 9 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span><span class="n">T</span> <span class="n">inv</span><span class="p">(</span><span class="n">T</span> <span class="n">a</span><span class="p">,</span><span class="n">T</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="10 "></span>    <span class="n">T</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">;</span><span class="n">exgcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="11 "></span>    <span class="k">return</span> <span class="p">(</span><span class="n">x</span><span class="o">%</span><span class="n">b</span><span class="o">+</span><span class="n">b</span><span class="p">)</span><span class="o">%</span><span class="n">b</span><span class="p">;</span>
<span class="linenos" data-linenos="12 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
<h2 id="bsgs">BSGS</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span><span class="n">T</span> <span class="n">bsgs</span><span class="p">(</span><span class="n">T</span> <span class="n">a</span><span class="p">,</span><span class="n">T</span> <span class="n">b</span><span class="p">,</span><span class="n">T</span> <span class="n">p</span><span class="p">){</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="n">unordered_map</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="n">T</span><span class="o">&gt;</span><span class="n">hash</span><span class="p">;</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="n">b</span><span class="o">%=</span><span class="n">p</span><span class="p">;</span><span class="n">T</span> <span class="n">t</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="k">for</span><span class="p">(</span><span class="n">T</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">t</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos=" 5 "></span>        <span class="n">hash</span><span class="p">[</span><span class="n">b</span><span class="o">*</span><span class="n">ksm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">i</span><span class="p">,</span><span class="n">p</span><span class="p">)</span><span class="o">%</span><span class="n">p</span><span class="p">]</span><span class="o">=</span><span class="n">i</span><span class="p">;</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">ksm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">t</span><span class="p">,</span><span class="n">p</span><span class="p">);</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">a</span><span class="p">)</span><span class="k">return</span> <span class="n">b</span><span class="o">=</span><span class="mi">0</span><span class="o">?</span><span class="mi">1</span><span class="o">:</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="k">for</span><span class="p">(</span><span class="n">T</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;=</span><span class="n">t</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="linenos" data-linenos=" 9 "></span>        <span class="n">T</span> <span class="n">val</span><span class="o">=</span><span class="n">ksm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">i</span><span class="p">,</span><span class="n">p</span><span class="p">);</span>
<span class="linenos" data-linenos="10 "></span>        <span class="kt">int</span> <span class="n">j</span><span class="o">=</span><span class="n">hash</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="o">==</span><span class="n">hash</span><span class="p">.</span><span class="n">end</span><span class="p">()</span><span class="o">?</span><span class="mi">-1</span><span class="o">:</span><span class="n">hash</span><span class="p">[</span><span class="n">val</span><span class="p">];</span>
<span class="linenos" data-linenos="11 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">j</span><span class="o">&gt;=</span><span class="mi">0</span><span class="o">&amp;&amp;</span><span class="n">i</span><span class="o">*</span><span class="n">t</span><span class="o">-</span><span class="n">j</span><span class="o">&gt;=</span><span class="mi">0</span><span class="p">)</span><span class="k">return</span> <span class="n">i</span><span class="o">*</span><span class="n">t</span><span class="o">-</span><span class="n">j</span><span class="p">;</span>
<span class="linenos" data-linenos="12 "></span>    <span class="p">}</span><span class="k">return</span> <span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
<h2 id="exbsgs">exBSGS</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span><span class="n">T</span> <span class="n">ex_bsgs</span><span class="p">(</span><span class="n">T</span> <span class="n">a</span><span class="p">,</span><span class="n">T</span> <span class="n">b</span><span class="p">,</span><span class="n">T</span> <span class="n">p</span><span class="p">){</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">==</span><span class="mi">1</span><span class="o">||</span><span class="n">p</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span><span class="k">return</span> <span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="n">T</span> <span class="n">na</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="k">while</span><span class="p">(</span><span class="n">T</span> <span class="n">g</span><span class="o">=</span><span class="n">__gcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">p</span><span class="p">)){</span>
<span class="linenos" data-linenos=" 5 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">g</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span><span class="k">break</span><span class="p">;</span>
<span class="linenos" data-linenos=" 6 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">%</span><span class="n">g</span><span class="p">)</span><span class="k">return</span> <span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="n">k</span><span class="o">++</span><span class="p">;</span><span class="n">b</span><span class="o">/=</span><span class="n">g</span><span class="p">;</span><span class="n">p</span><span class="o">/=</span><span class="n">g</span><span class="p">;</span><span class="n">na</span><span class="o">=</span><span class="n">na</span><span class="o">*</span><span class="p">(</span><span class="n">a</span><span class="o">/</span><span class="n">g</span><span class="p">)</span><span class="o">%</span><span class="n">p</span><span class="p">;</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">na</span><span class="o">==</span><span class="n">b</span><span class="p">)</span><span class="k">return</span> <span class="n">k</span><span class="p">;</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="10 "></span>    <span class="kt">long</span> <span class="kt">long</span> <span class="n">f</span><span class="o">=</span><span class="n">bsgs</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="o">*</span><span class="n">inv</span><span class="p">(</span><span class="n">na</span><span class="p">,</span><span class="n">p</span><span class="p">)</span><span class="o">%</span><span class="n">p</span><span class="p">,</span><span class="n">p</span><span class="p">);</span>
<span class="linenos" data-linenos="11 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">f</span><span class="o">==</span><span class="mi">-1</span><span class="p">)</span><span class="k">return</span> <span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="12 "></span>    <span class="k">return</span> <span class="n">f</span><span class="o">+</span><span class="n">k</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
<h2 id="_11">二次剩余</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">namespace</span> <span class="n">residue</span><span class="p">{</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="c1">//求二次剩余</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="kt">int</span> <span class="n">mod</span><span class="p">,</span><span class="n">I2</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="k">struct</span> <span class="nc">complex</span><span class="p">{</span>
<span class="linenos" data-linenos=" 5 "></span>        <span class="kt">long</span> <span class="kt">long</span> <span class="n">real</span><span class="p">,</span><span class="n">imag</span><span class="p">;</span>
<span class="linenos" data-linenos=" 6 "></span>        <span class="n">complex</span><span class="o">&amp;</span><span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">x</span><span class="p">){</span><span class="n">real</span><span class="o">=</span><span class="n">x</span><span class="p">,</span><span class="n">imag</span><span class="o">=</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="n">complex</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">real</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">imag</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">:</span><span class="n">real</span><span class="p">(</span><span class="n">real</span><span class="p">),</span><span class="n">imag</span><span class="p">(</span><span class="n">imag</span><span class="p">){}</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="kt">bool</span> <span class="k">operator</span><span class="o">==</span><span class="p">(</span><span class="k">const</span> <span class="n">complex</span><span class="o">&amp;</span><span class="n">b</span><span class="p">)</span><span class="k">const</span><span class="p">{</span><span class="k">return</span> <span class="n">real</span><span class="o">==</span><span class="n">b</span><span class="p">.</span><span class="n">real</span><span class="o">&amp;&amp;</span><span class="n">imag</span><span class="o">==</span><span class="n">b</span><span class="p">.</span><span class="n">imag</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 9 "></span>        <span class="n">complex</span> <span class="k">operator</span><span class="o">*</span><span class="p">(</span><span class="k">const</span> <span class="n">complex</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">)</span><span class="k">const</span><span class="p">{</span><span class="k">return</span> <span class="nf">complex</span><span class="p">((</span><span class="n">real</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">real</span><span class="o">+</span><span class="n">I2</span><span class="o">*</span><span class="n">imag</span><span class="o">%</span><span class="n">mod</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">imag</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">,(</span><span class="n">real</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">imag</span><span class="o">+</span><span class="n">imag</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">real</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">);}</span>
<span class="linenos" data-linenos="10 "></span>    <span class="p">};</span>
<span class="linenos" data-linenos="11 "></span>    <span class="n">complex</span> <span class="nf">ksm</span><span class="p">(</span><span class="n">complex</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="12 "></span>        <span class="n">complex</span> <span class="n">res</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="14 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="n">res</span><span class="o">=</span><span class="n">res</span><span class="o">*</span><span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="15 "></span>            <span class="n">a</span><span class="o">=</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="p">;</span><span class="n">b</span><span class="o">&gt;&gt;=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="16 "></span>        <span class="p">}</span><span class="k">return</span> <span class="n">res</span><span class="p">;</span>
<span class="linenos" data-linenos="17 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="18 "></span>    <span class="kt">void</span> <span class="nf">init</span><span class="p">(</span><span class="kt">int</span> <span class="n">p</span><span class="p">){</span><span class="n">mod</span><span class="o">=</span><span class="n">p</span><span class="p">;}</span>
<span class="linenos" data-linenos="19 "></span>    <span class="kt">bool</span> <span class="nf">check</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span><span class="k">return</span> <span class="n">ksm</span><span class="p">(</span><span class="n">complex</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">0</span><span class="p">),(</span><span class="n">mod</span><span class="mi">-1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">).</span><span class="n">real</span><span class="o">==</span><span class="mi">1</span><span class="p">;}</span>
<span class="linenos" data-linenos="20 "></span>    <span class="n">pair</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">solve</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span>
<span class="linenos" data-linenos="21 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span><span class="k">return</span> <span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">};</span>
<span class="linenos" data-linenos="22 "></span>        <span class="kt">long</span> <span class="kt">long</span> <span class="n">a</span><span class="o">=</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos="23 "></span>        <span class="k">while</span><span class="p">(</span><span class="o">!</span><span class="n">a</span><span class="o">||</span><span class="n">check</span><span class="p">((</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">+</span><span class="n">mod</span><span class="o">-</span><span class="n">n</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">))</span><span class="n">a</span><span class="o">=</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos="24 "></span>        <span class="n">I2</span><span class="o">=</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">+</span><span class="n">mod</span><span class="o">-</span><span class="n">n</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos="25 "></span>        <span class="kt">int</span> <span class="n">x0</span><span class="o">=</span><span class="n">ksm</span><span class="p">(</span><span class="n">complex</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="mi">1</span><span class="p">),(</span><span class="n">mod</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">).</span><span class="n">real</span><span class="p">;</span>
<span class="linenos" data-linenos="26 "></span>        <span class="k">return</span> <span class="p">{</span><span class="n">min</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span><span class="n">mod</span><span class="o">-</span><span class="n">x0</span><span class="p">),</span><span class="n">max</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span><span class="n">mod</span><span class="o">-</span><span class="n">x0</span><span class="p">)};</span>
<span class="linenos" data-linenos="27 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="28 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
<h1 id="_12">数据结构</h1>
<h2 id="_13">并查集</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="1 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">maxn</span><span class="o">=</span><span class="mf">1e4</span><span class="o">+</span><span class="mi">10</span><span class="p">;</span>
<span class="linenos" data-linenos="2 "></span><span class="k">struct</span> <span class="nc">DSU</span><span class="p">{</span>
<span class="linenos" data-linenos="3 "></span>    <span class="kt">int</span> <span class="n">fa</span><span class="p">[</span><span class="n">maxn</span><span class="p">];</span>
<span class="linenos" data-linenos="4 "></span>    <span class="n">DSU</span><span class="p">(){</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">maxn</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">fa</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">i</span><span class="p">;}</span>
<span class="linenos" data-linenos="5 "></span>    <span class="kt">int</span> <span class="n">getfa</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">==</span><span class="n">fa</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">?</span><span class="nl">x</span><span class="p">:</span><span class="n">fa</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">getfa</span><span class="p">(</span><span class="n">fa</span><span class="p">[</span><span class="n">x</span><span class="p">]);}</span>
<span class="linenos" data-linenos="6 "></span>    <span class="kt">void</span> <span class="n">merge</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">y</span><span class="p">){</span><span class="n">fa</span><span class="p">[</span><span class="n">getfa</span><span class="p">(</span><span class="n">y</span><span class="p">)]</span><span class="o">=</span><span class="n">getfa</span><span class="p">(</span><span class="n">x</span><span class="p">);}</span>
<span class="linenos" data-linenos="7 "></span><span class="p">};</span>
</code></pre></div>
</div></div>
<h2 id="_14">可持久化线段树</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">struct</span> <span class="nc">node</span><span class="p">{</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="kt">int</span> <span class="n">l</span><span class="p">,</span><span class="n">r</span><span class="p">,</span><span class="n">lc</span><span class="p">,</span><span class="n">rc</span><span class="p">;</span><span class="kt">int</span> <span class="n">len</span><span class="p">,</span><span class="n">llen</span><span class="p">,</span><span class="n">rlen</span><span class="p">,</span><span class="n">mlen</span><span class="p">;</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span><span class="n">node</span><span class="p">(</span><span class="n">T</span> <span class="n">val</span><span class="p">){</span>
<span class="linenos" data-linenos=" 4 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">val</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span><span class="n">llen</span><span class="o">=</span><span class="n">rlen</span><span class="o">=</span><span class="n">mlen</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 5 "></span>        <span class="k">else</span> <span class="n">llen</span><span class="o">=</span><span class="n">rlen</span><span class="o">=</span><span class="n">mlen</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 6 "></span>        <span class="n">len</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="n">node</span><span class="p">(){</span><span class="n">llen</span><span class="o">=</span><span class="n">rlen</span><span class="o">=</span><span class="n">mlen</span><span class="o">=</span><span class="n">len</span><span class="o">=</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="n">node</span> <span class="k">operator</span><span class="o">+</span><span class="p">(</span><span class="k">const</span> <span class="n">node</span> <span class="n">b</span><span class="p">)</span><span class="k">const</span><span class="p">{</span>
<span class="linenos" data-linenos="10 "></span>        <span class="n">node</span> <span class="n">c</span><span class="p">;</span><span class="n">c</span><span class="p">.</span><span class="n">len</span><span class="o">=</span><span class="n">len</span><span class="o">+</span><span class="n">b</span><span class="p">.</span><span class="n">len</span><span class="p">;</span>
<span class="linenos" data-linenos="11 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">r</span><span class="o">-</span><span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="o">==</span><span class="n">mlen</span><span class="p">)</span><span class="n">c</span><span class="p">.</span><span class="n">llen</span><span class="o">=</span><span class="n">mlen</span><span class="o">+</span><span class="n">b</span><span class="p">.</span><span class="n">llen</span><span class="p">;</span>
<span class="linenos" data-linenos="12 "></span>        <span class="k">else</span> <span class="n">c</span><span class="p">.</span><span class="n">llen</span><span class="o">=</span><span class="n">llen</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="n">r</span><span class="o">-</span><span class="n">b</span><span class="p">.</span><span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="o">==</span><span class="n">b</span><span class="p">.</span><span class="n">mlen</span><span class="p">)</span><span class="n">c</span><span class="p">.</span><span class="n">rlen</span><span class="o">=</span><span class="n">b</span><span class="p">.</span><span class="n">mlen</span><span class="o">+</span><span class="n">rlen</span><span class="p">;</span>
<span class="linenos" data-linenos="14 "></span>        <span class="k">else</span> <span class="n">c</span><span class="p">.</span><span class="n">rlen</span><span class="o">=</span><span class="n">b</span><span class="p">.</span><span class="n">rlen</span><span class="p">;</span>
<span class="linenos" data-linenos="15 "></span>        <span class="n">c</span><span class="p">.</span><span class="n">mlen</span><span class="o">=</span><span class="n">max</span><span class="p">(</span><span class="n">rlen</span><span class="o">+</span><span class="n">b</span><span class="p">.</span><span class="n">llen</span><span class="p">,</span><span class="n">max</span><span class="p">(</span><span class="n">mlen</span><span class="p">,</span><span class="n">b</span><span class="p">.</span><span class="n">mlen</span><span class="p">));</span>
<span class="linenos" data-linenos="16 "></span>        <span class="k">return</span> <span class="n">c</span><span class="p">;</span>
<span class="linenos" data-linenos="17 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="18 "></span>    <span class="kt">void</span> <span class="n">cpy</span><span class="p">(</span><span class="n">node</span> <span class="n">from</span><span class="p">){</span>
<span class="linenos" data-linenos="19 "></span>        <span class="n">llen</span><span class="o">=</span><span class="n">from</span><span class="p">.</span><span class="n">llen</span><span class="p">;</span>
<span class="linenos" data-linenos="20 "></span>        <span class="n">rlen</span><span class="o">=</span><span class="n">from</span><span class="p">.</span><span class="n">rlen</span><span class="p">;</span>
<span class="linenos" data-linenos="21 "></span>        <span class="n">mlen</span><span class="o">=</span><span class="n">from</span><span class="p">.</span><span class="n">mlen</span><span class="p">;</span>
<span class="linenos" data-linenos="22 "></span>        <span class="n">len</span><span class="o">=</span><span class="n">from</span><span class="p">.</span><span class="n">len</span><span class="p">;</span>
<span class="linenos" data-linenos="23 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="24 "></span><span class="p">};</span>
<span class="linenos" data-linenos="25 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">A</span><span class="p">,</span><span class="k">class</span> <span class="nc">B</span><span class="o">&gt;</span>
<span class="linenos" data-linenos="26 "></span><span class="k">struct</span> <span class="nc">HJT_Tree</span><span class="p">{</span>
<span class="linenos" data-linenos="27 "></span>    <span class="n">A</span> <span class="n">t</span><span class="p">[</span><span class="n">maxn</span><span class="o">*</span><span class="mi">20</span><span class="p">];</span><span class="n">B</span> <span class="n">a</span><span class="p">[</span><span class="n">maxn</span><span class="p">];</span>
<span class="linenos" data-linenos="28 "></span>    <span class="n">B</span> <span class="o">*</span><span class="k">operator</span><span class="p">[](</span><span class="k">const</span> <span class="kt">int</span> <span class="n">x</span><span class="p">){</span><span class="k">return</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">[</span><span class="n">x</span><span class="p">];}</span>
<span class="linenos" data-linenos="29 "></span>    <span class="kt">int</span> <span class="n">cnt</span><span class="p">;</span><span class="n">HJT_Tree</span><span class="p">(){</span><span class="n">cnt</span><span class="o">=</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos="30 "></span>    <span class="kt">int</span> <span class="n">build</span><span class="p">(</span><span class="kt">int</span> <span class="n">l</span><span class="p">,</span><span class="kt">int</span> <span class="n">r</span><span class="p">){</span>
<span class="linenos" data-linenos="31 "></span>        <span class="kt">int</span> <span class="n">x</span><span class="o">=++</span><span class="n">cnt</span><span class="p">;</span>
<span class="linenos" data-linenos="32 "></span>        <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">l</span><span class="o">=</span><span class="n">l</span><span class="p">,</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">r</span><span class="o">=</span><span class="n">r</span><span class="p">;</span>
<span class="linenos" data-linenos="33 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">l</span><span class="o">==</span><span class="n">r</span><span class="p">){</span>
<span class="linenos" data-linenos="34 "></span>            <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">cpy</span><span class="p">(</span><span class="n">A</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">l</span><span class="p">]));</span>
<span class="linenos" data-linenos="35 "></span>            <span class="k">return</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="36 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="37 "></span>        <span class="kt">int</span> <span class="n">mid</span><span class="o">=</span><span class="n">l</span><span class="o">+</span><span class="n">r</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="38 "></span>        <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="o">=</span><span class="n">build</span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="n">mid</span><span class="p">);</span>
<span class="linenos" data-linenos="39 "></span>        <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="o">=</span><span class="n">build</span><span class="p">(</span><span class="n">mid</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">r</span><span class="p">);</span>
<span class="linenos" data-linenos="40 "></span>        <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">cpy</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">]</span><span class="o">+</span><span class="n">t</span><span class="p">[</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="p">]);</span>
<span class="linenos" data-linenos="41 "></span>        <span class="k">return</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="42 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="43 "></span>    <span class="kt">int</span> <span class="n">upd</span><span class="p">(</span><span class="kt">int</span> <span class="n">lst</span><span class="p">,</span><span class="kt">int</span> <span class="n">pos</span><span class="p">,</span><span class="n">B</span> <span class="n">val</span><span class="p">){</span>
<span class="linenos" data-linenos="44 "></span>        <span class="kt">int</span> <span class="n">x</span><span class="o">=++</span><span class="n">cnt</span><span class="p">;</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">t</span><span class="p">[</span><span class="n">lst</span><span class="p">];</span>
<span class="linenos" data-linenos="45 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">l</span><span class="o">==</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">r</span><span class="p">){</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">cpy</span><span class="p">(</span><span class="n">A</span><span class="p">(</span><span class="n">val</span><span class="p">));</span><span class="k">return</span> <span class="n">x</span><span class="p">;}</span>
<span class="linenos" data-linenos="46 "></span>        <span class="kt">int</span> <span class="n">mid</span><span class="o">=</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">l</span><span class="o">+</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">r</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="47 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">pos</span><span class="o">&lt;=</span><span class="n">mid</span><span class="p">)</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="o">=</span><span class="n">upd</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">lst</span><span class="p">].</span><span class="n">lc</span><span class="p">,</span><span class="n">pos</span><span class="p">,</span><span class="n">val</span><span class="p">);</span>
<span class="linenos" data-linenos="48 "></span>        <span class="k">else</span> <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="o">=</span><span class="n">upd</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">lst</span><span class="p">].</span><span class="n">rc</span><span class="p">,</span><span class="n">pos</span><span class="p">,</span><span class="n">val</span><span class="p">);</span>
<span class="linenos" data-linenos="49 "></span>        <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">cpy</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">]</span><span class="o">+</span><span class="n">t</span><span class="p">[</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="p">]);</span>
<span class="linenos" data-linenos="50 "></span>        <span class="k">return</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="51 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="52 "></span>    <span class="n">A</span> <span class="n">qry</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">lq</span><span class="p">,</span><span class="kt">int</span> <span class="n">rq</span><span class="p">){</span>
<span class="linenos" data-linenos="53 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">lq</span><span class="o">&lt;=</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">l</span><span class="o">&amp;&amp;</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">r</span><span class="o">&lt;=</span><span class="n">rq</span><span class="p">)</span><span class="k">return</span> <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">];</span>
<span class="linenos" data-linenos="54 "></span>        <span class="kt">int</span> <span class="n">mid</span><span class="o">=</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">l</span><span class="o">+</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">r</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="55 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">rq</span><span class="o">&lt;=</span><span class="n">mid</span><span class="p">)</span><span class="k">return</span> <span class="n">qry</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">,</span><span class="n">lq</span><span class="p">,</span><span class="n">rq</span><span class="p">);</span>
<span class="linenos" data-linenos="56 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">lq</span><span class="o">&gt;</span><span class="n">mid</span><span class="p">)</span><span class="k">return</span> <span class="n">qry</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="p">,</span><span class="n">lq</span><span class="p">,</span><span class="n">rq</span><span class="p">);</span>
<span class="linenos" data-linenos="57 "></span>        <span class="k">return</span> <span class="nf">qry</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">,</span><span class="n">lq</span><span class="p">,</span><span class="n">rq</span><span class="p">)</span><span class="o">+</span><span class="n">qry</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="p">,</span><span class="n">lq</span><span class="p">,</span><span class="n">rq</span><span class="p">);</span>
<span class="linenos" data-linenos="58 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="59 "></span><span class="p">};</span>
<span class="linenos" data-linenos="60 "></span><span class="n">HJT_Tree</span><span class="o">&lt;</span><span class="n">node</span><span class="p">,</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">Tree</span><span class="p">;</span>
</code></pre></div>
</div></div>
<h2 id="splay">Splay</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="  1 "></span><span class="cp">#define clear(x) {f[x]=key[x]=size[x]=recy[x]=son[x][0]=son[x][1]=0;}</span>
<span class="linenos" data-linenos="  2 "></span><span class="cp">#define get(x) (son[f[x]][1]==x)</span>
<span class="linenos" data-linenos="  3 "></span><span class="cp">#define connect(x,y,p) {if(x)f[x]=y;if(y)son[y][p]=x;}</span>
<span class="linenos" data-linenos="  4 "></span><span class="cp">#define update(x) {size[x]=recy[x];size[x]+=size[son[x][0]]+size[son[x][1]];}</span>
<span class="linenos" data-linenos="  5 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">const</span> <span class="kt">int</span> <span class="n">N</span><span class="o">&gt;</span>
<span class="linenos" data-linenos="  6 "></span><span class="k">struct</span> <span class="nc">Splay</span><span class="p">{</span>
<span class="linenos" data-linenos="  7 "></span>    <span class="kt">int</span> <span class="n">num</span><span class="p">,</span><span class="n">root</span><span class="p">,</span><span class="n">f</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">key</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">size</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">recy</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">son</span><span class="p">[</span><span class="n">N</span><span class="p">][</span><span class="mi">2</span><span class="p">];</span>
<span class="linenos" data-linenos="  8 "></span>    <span class="kt">void</span> <span class="nf">rotate</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="  9 "></span>        <span class="kt">int</span> <span class="n">fa</span><span class="o">=</span><span class="n">f</span><span class="p">[</span><span class="n">x</span><span class="p">],</span><span class="n">ffa</span><span class="o">=</span><span class="n">f</span><span class="p">[</span><span class="n">fa</span><span class="p">],</span><span class="n">p1</span><span class="o">=</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="n">p2</span><span class="o">=</span><span class="n">get</span><span class="p">(</span><span class="n">fa</span><span class="p">);</span>
<span class="linenos" data-linenos=" 10 "></span>        <span class="n">connect</span><span class="p">(</span><span class="n">son</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">p1</span><span class="o">^</span><span class="mi">1</span><span class="p">],</span><span class="n">fa</span><span class="p">,</span><span class="n">p1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 11 "></span>        <span class="n">connect</span><span class="p">(</span><span class="n">fa</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">p1</span><span class="o">^</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 12 "></span>        <span class="n">connect</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">ffa</span><span class="p">,</span><span class="n">p2</span><span class="p">);</span>
<span class="linenos" data-linenos=" 13 "></span>        <span class="n">update</span><span class="p">(</span><span class="n">fa</span><span class="p">);</span><span class="n">update</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos=" 14 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 15 "></span>    <span class="kt">void</span> <span class="nf">splay</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 16 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">fa</span><span class="p">;</span><span class="n">fa</span><span class="o">=</span><span class="n">f</span><span class="p">[</span><span class="n">x</span><span class="p">];</span><span class="n">rotate</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="linenos" data-linenos=" 17 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">fa</span><span class="p">])</span><span class="n">rotate</span><span class="p">(</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">==</span><span class="n">get</span><span class="p">(</span><span class="n">fa</span><span class="p">)</span><span class="o">?</span><span class="nl">fa</span><span class="p">:</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos=" 18 "></span>        <span class="n">root</span><span class="o">=</span><span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos=" 19 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 20 "></span>    <span class="kt">void</span> <span class="nf">insert</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 21 "></span>        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">root</span><span class="p">){</span>
<span class="linenos" data-linenos=" 22 "></span>            <span class="n">root</span><span class="o">=++</span><span class="n">num</span><span class="p">;</span>
<span class="linenos" data-linenos=" 23 "></span>            <span class="n">key</span><span class="p">[</span><span class="n">root</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="p">;</span><span class="n">size</span><span class="p">[</span><span class="n">root</span><span class="p">]</span><span class="o">=</span><span class="n">recy</span><span class="p">[</span><span class="n">root</span><span class="p">]</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 24 "></span>            <span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 25 "></span>            <span class="k">return</span><span class="p">;</span>
<span class="linenos" data-linenos=" 26 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 27 "></span>        <span class="kt">int</span> <span class="n">now</span><span class="o">=</span><span class="n">root</span><span class="p">,</span><span class="n">fa</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 28 "></span>        <span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
<span class="linenos" data-linenos=" 29 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">key</span><span class="p">[</span><span class="n">now</span><span class="p">]</span><span class="o">==</span><span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 30 "></span>                <span class="o">++</span><span class="n">recy</span><span class="p">[</span><span class="n">now</span><span class="p">];</span>
<span class="linenos" data-linenos=" 31 "></span>                <span class="n">update</span><span class="p">(</span><span class="n">now</span><span class="p">);</span><span class="n">update</span><span class="p">(</span><span class="n">fa</span><span class="p">);</span>
<span class="linenos" data-linenos=" 32 "></span>                <span class="n">splay</span><span class="p">(</span><span class="n">now</span><span class="p">);</span><span class="k">return</span><span class="p">;</span>
<span class="linenos" data-linenos=" 33 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos=" 34 "></span>            <span class="n">fa</span><span class="o">=</span><span class="n">now</span><span class="p">,</span><span class="n">now</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="n">x</span><span class="o">&gt;</span><span class="n">key</span><span class="p">[</span><span class="n">now</span><span class="p">]];</span>
<span class="linenos" data-linenos=" 35 "></span>            <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">now</span><span class="p">){</span>
<span class="linenos" data-linenos=" 36 "></span>                <span class="n">key</span><span class="p">[</span><span class="o">++</span><span class="n">num</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos=" 37 "></span>                <span class="n">size</span><span class="p">[</span><span class="n">num</span><span class="p">]</span><span class="o">=</span><span class="n">recy</span><span class="p">[</span><span class="n">num</span><span class="p">]</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 38 "></span>                <span class="n">f</span><span class="p">[</span><span class="n">num</span><span class="p">]</span><span class="o">=</span><span class="n">fa</span><span class="p">;</span><span class="n">son</span><span class="p">[</span><span class="n">fa</span><span class="p">][</span><span class="n">x</span><span class="o">&gt;</span><span class="n">key</span><span class="p">[</span><span class="n">fa</span><span class="p">]]</span><span class="o">=</span><span class="n">num</span><span class="p">;</span>
<span class="linenos" data-linenos=" 39 "></span>                <span class="n">update</span><span class="p">(</span><span class="n">fa</span><span class="p">);</span><span class="n">splay</span><span class="p">(</span><span class="n">num</span><span class="p">);</span>
<span class="linenos" data-linenos=" 40 "></span>                <span class="k">return</span><span class="p">;</span>
<span class="linenos" data-linenos=" 41 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos=" 42 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 43 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 44 "></span>    <span class="kt">int</span> <span class="nf">find</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 45 "></span>        <span class="kt">int</span> <span class="n">now</span><span class="o">=</span><span class="n">root</span><span class="p">,</span><span class="n">ans</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 46 "></span>        <span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
<span class="linenos" data-linenos=" 47 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">x</span><span class="o">&lt;</span><span class="n">key</span><span class="p">[</span><span class="n">now</span><span class="p">]){</span>
<span class="linenos" data-linenos=" 48 "></span>                <span class="n">now</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">0</span><span class="p">];</span>
<span class="linenos" data-linenos=" 49 "></span>                <span class="k">continue</span><span class="p">;</span>
<span class="linenos" data-linenos=" 50 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos=" 51 "></span>            <span class="n">ans</span><span class="o">+=</span><span class="n">size</span><span class="p">[</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">0</span><span class="p">]];</span>
<span class="linenos" data-linenos=" 52 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">x</span><span class="o">==</span><span class="n">key</span><span class="p">[</span><span class="n">now</span><span class="p">]){</span><span class="n">splay</span><span class="p">(</span><span class="n">now</span><span class="p">);</span><span class="k">return</span> <span class="n">ans</span><span class="o">+</span><span class="mi">1</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 53 "></span>            <span class="n">ans</span><span class="o">+=</span><span class="n">recy</span><span class="p">[</span><span class="n">now</span><span class="p">];</span><span class="n">now</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">1</span><span class="p">];</span>
<span class="linenos" data-linenos=" 54 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 55 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 56 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">kth</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 57 "></span>        <span class="kt">int</span> <span class="n">now</span><span class="o">=</span><span class="n">root</span><span class="p">;</span>
<span class="linenos" data-linenos=" 58 "></span>        <span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
<span class="linenos" data-linenos=" 59 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">&amp;&amp;</span><span class="n">x</span><span class="o">&lt;=</span><span class="n">size</span><span class="p">[</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">0</span><span class="p">]]){</span>
<span class="linenos" data-linenos=" 60 "></span>            <span class="n">now</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">0</span><span class="p">];</span>
<span class="linenos" data-linenos=" 61 "></span>            <span class="k">continue</span><span class="p">;</span>
<span class="linenos" data-linenos=" 62 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 63 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span><span class="n">x</span><span class="o">-=</span><span class="n">size</span><span class="p">[</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">0</span><span class="p">]];</span>
<span class="linenos" data-linenos=" 64 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">x</span><span class="o">&lt;=</span><span class="n">recy</span><span class="p">[</span><span class="n">now</span><span class="p">])</span><span class="k">return</span> <span class="n">key</span><span class="p">[</span><span class="n">now</span><span class="p">];</span>
<span class="linenos" data-linenos=" 65 "></span>        <span class="n">x</span><span class="o">-=</span><span class="n">recy</span><span class="p">[</span><span class="n">now</span><span class="p">];</span><span class="n">now</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">1</span><span class="p">];</span>
<span class="linenos" data-linenos=" 66 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 67 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 68 "></span>    <span class="kt">int</span> <span class="n">pre</span><span class="p">(){</span>
<span class="linenos" data-linenos=" 69 "></span>        <span class="kt">int</span> <span class="n">now</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">0</span><span class="p">];</span>
<span class="linenos" data-linenos=" 70 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span><span class="n">now</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">1</span><span class="p">];</span>
<span class="linenos" data-linenos=" 71 "></span>        <span class="k">return</span> <span class="n">now</span><span class="p">;</span>
<span class="linenos" data-linenos=" 72 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 73 "></span>    <span class="kt">int</span> <span class="n">nxt</span><span class="p">(){</span>
<span class="linenos" data-linenos=" 74 "></span>        <span class="kt">int</span> <span class="n">now</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">1</span><span class="p">];</span>
<span class="linenos" data-linenos=" 75 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span><span class="n">now</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">now</span><span class="p">][</span><span class="mi">0</span><span class="p">];</span>
<span class="linenos" data-linenos=" 76 "></span>        <span class="k">return</span> <span class="n">now</span><span class="p">;</span>
<span class="linenos" data-linenos=" 77 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 78 "></span>    <span class="kt">void</span> <span class="n">del</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 79 "></span>        <span class="n">find</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos=" 80 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">recy</span><span class="p">[</span><span class="n">root</span><span class="p">]</span><span class="o">&gt;</span><span class="mi">1</span><span class="p">){</span>
<span class="linenos" data-linenos=" 81 "></span>            <span class="n">recy</span><span class="p">[</span><span class="n">root</span><span class="p">]</span><span class="o">--</span><span class="p">;</span>
<span class="linenos" data-linenos=" 82 "></span>            <span class="n">update</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
<span class="linenos" data-linenos=" 83 "></span>            <span class="k">return</span><span class="p">;</span>
<span class="linenos" data-linenos=" 84 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 85 "></span>        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">&amp;&amp;!</span><span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">1</span><span class="p">]){</span>
<span class="linenos" data-linenos=" 86 "></span>            <span class="n">clear</span><span class="p">(</span><span class="n">root</span><span class="p">);</span><span class="n">root</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 87 "></span>            <span class="k">return</span><span class="p">;</span>
<span class="linenos" data-linenos=" 88 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 89 "></span>        <span class="kt">int</span> <span class="n">tmp</span><span class="o">=</span><span class="n">root</span><span class="p">;</span>
<span class="linenos" data-linenos=" 90 "></span>        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">0</span><span class="p">]){</span>
<span class="linenos" data-linenos=" 91 "></span>            <span class="n">f</span><span class="p">[</span><span class="n">root</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">1</span><span class="p">]]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 92 "></span>            <span class="n">clear</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span><span class="k">return</span><span class="p">;</span>
<span class="linenos" data-linenos=" 93 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 94 "></span>        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">1</span><span class="p">]){</span>
<span class="linenos" data-linenos=" 95 "></span>            <span class="n">f</span><span class="p">[</span><span class="n">root</span><span class="o">=</span><span class="n">son</span><span class="p">[</span><span class="n">root</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 96 "></span>            <span class="n">clear</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span><span class="k">return</span><span class="p">;</span>
<span class="linenos" data-linenos=" 97 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 98 "></span>        <span class="kt">int</span> <span class="n">left</span><span class="o">=</span><span class="n">pre</span><span class="p">();</span>
<span class="linenos" data-linenos=" 99 "></span>        <span class="n">splay</span><span class="p">(</span><span class="n">left</span><span class="p">);</span><span class="n">connect</span><span class="p">(</span><span class="n">son</span><span class="p">[</span><span class="n">tmp</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span><span class="n">root</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="100 "></span>        <span class="n">clear</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span><span class="n">update</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
<span class="linenos" data-linenos="101 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="102 "></span><span class="p">};</span>
</code></pre></div>
</div></div>
<h2 id="lct">LCT</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="cp">#define lc c[x][0]</span>
<span class="linenos" data-linenos=" 2 "></span><span class="cp">#define rc c[x][1]</span>
<span class="linenos" data-linenos=" 3 "></span><span class="k">struct</span> <span class="nc">Link_Cut_Tree</span><span class="p">{</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="kt">int</span> <span class="n">f</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">c</span><span class="p">[</span><span class="n">N</span><span class="p">][</span><span class="mi">2</span><span class="p">],</span><span class="n">v</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">s</span><span class="p">[</span><span class="n">N</span><span class="p">];</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="kt">bool</span> <span class="n">r</span><span class="p">[</span><span class="n">N</span><span class="p">];</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="kt">bool</span> <span class="nf">nroot</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="k">return</span> <span class="n">c</span><span class="p">[</span><span class="n">f</span><span class="p">[</span><span class="n">x</span><span class="p">]][</span><span class="mi">0</span><span class="p">]</span><span class="o">==</span><span class="n">x</span><span class="o">||</span><span class="n">c</span><span class="p">[</span><span class="n">f</span><span class="p">[</span><span class="n">x</span><span class="p">]][</span><span class="mi">1</span><span class="p">]</span><span class="o">==</span><span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="kt">void</span> <span class="nf">pushup</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="10 "></span>        <span class="n">s</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">max</span><span class="p">(</span><span class="n">v</span><span class="p">[</span><span class="n">x</span><span class="p">],</span><span class="n">max</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="n">lc</span><span class="p">],</span><span class="n">s</span><span class="p">[</span><span class="n">rc</span><span class="p">]));</span>
<span class="linenos" data-linenos="11 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="12 "></span>    <span class="kt">void</span> <span class="nf">pushr</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="13 "></span>        <span class="n">swap</span><span class="p">(</span><span class="n">lc</span><span class="p">,</span><span class="n">rc</span><span class="p">);</span>
<span class="linenos" data-linenos="14 "></span>        <span class="n">r</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">^=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="15 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="16 "></span>    <span class="kt">void</span> <span class="nf">pushdown</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="17 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="n">x</span><span class="p">]){</span>
<span class="linenos" data-linenos="18 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">lc</span><span class="p">)</span><span class="n">pushr</span><span class="p">(</span><span class="n">lc</span><span class="p">);</span>
<span class="linenos" data-linenos="19 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">rc</span><span class="p">)</span><span class="n">pushr</span><span class="p">(</span><span class="n">rc</span><span class="p">);</span>
<span class="linenos" data-linenos="20 "></span>            <span class="n">r</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="21 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="22 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="23 "></span>    <span class="cp">#define get(x) (c[f[x]][1]==x)</span>
<span class="linenos" data-linenos="24 "></span>    <span class="kt">void</span> <span class="nf">rotate</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="25 "></span>        <span class="kt">int</span> <span class="n">fa</span><span class="o">=</span><span class="n">f</span><span class="p">[</span><span class="n">x</span><span class="p">],</span><span class="n">ffa</span><span class="o">=</span><span class="n">f</span><span class="p">[</span><span class="n">fa</span><span class="p">],</span><span class="n">k</span><span class="o">=</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="n">w</span><span class="o">=</span><span class="n">c</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="o">!</span><span class="n">k</span><span class="p">];</span>
<span class="linenos" data-linenos="26 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">nroot</span><span class="p">(</span><span class="n">fa</span><span class="p">))</span><span class="n">c</span><span class="p">[</span><span class="n">ffa</span><span class="p">][</span><span class="n">get</span><span class="p">(</span><span class="n">fa</span><span class="p">)]</span><span class="o">=</span><span class="n">x</span><span class="p">;</span><span class="n">c</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="o">!</span><span class="n">k</span><span class="p">]</span><span class="o">=</span><span class="n">fa</span><span class="p">;</span><span class="n">c</span><span class="p">[</span><span class="n">fa</span><span class="p">][</span><span class="n">k</span><span class="p">]</span><span class="o">=</span><span class="n">w</span><span class="p">;</span>
<span class="linenos" data-linenos="27 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">w</span><span class="p">)</span><span class="n">f</span><span class="p">[</span><span class="n">w</span><span class="p">]</span><span class="o">=</span><span class="n">fa</span><span class="p">;</span><span class="n">f</span><span class="p">[</span><span class="n">fa</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="p">;</span><span class="n">f</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">ffa</span><span class="p">;</span><span class="n">pushup</span><span class="p">(</span><span class="n">fa</span><span class="p">);</span>
<span class="linenos" data-linenos="28 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="29 "></span>    <span class="kt">void</span> <span class="nf">splay</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="30 "></span>        <span class="kt">int</span> <span class="n">y</span><span class="o">=</span><span class="n">x</span><span class="p">;</span><span class="n">stack</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">s</span><span class="p">;</span>
<span class="linenos" data-linenos="31 "></span>        <span class="n">s</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="32 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">nroot</span><span class="p">(</span><span class="n">y</span><span class="p">))</span><span class="n">s</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">f</span><span class="p">[</span><span class="n">y</span><span class="p">]);</span>
<span class="linenos" data-linenos="33 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="n">size</span><span class="p">())</span><span class="n">pushdown</span><span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="n">top</span><span class="p">()),</span><span class="n">s</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span>
<span class="linenos" data-linenos="34 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">nroot</span><span class="p">(</span><span class="n">x</span><span class="p">)){</span>
<span class="linenos" data-linenos="35 "></span>            <span class="kt">int</span> <span class="n">fa</span><span class="o">=</span><span class="n">f</span><span class="p">[</span><span class="n">x</span><span class="p">],</span><span class="n">ffa</span><span class="o">=</span><span class="n">f</span><span class="p">[</span><span class="n">fa</span><span class="p">];</span>
<span class="linenos" data-linenos="36 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">nroot</span><span class="p">(</span><span class="n">fa</span><span class="p">))</span>
<span class="linenos" data-linenos="37 "></span>                <span class="n">rotate</span><span class="p">((</span><span class="o">!</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="o">^</span><span class="p">(</span><span class="o">!</span><span class="n">get</span><span class="p">(</span><span class="n">fa</span><span class="p">))</span><span class="o">?</span><span class="nl">x</span><span class="p">:</span><span class="n">fa</span><span class="p">);</span>
<span class="linenos" data-linenos="38 "></span>            <span class="n">rotate</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="39 "></span>        <span class="p">}</span><span class="n">pushup</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="40 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="41 "></span>    <span class="kt">void</span> <span class="nf">access</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="42 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">x</span><span class="p">;</span><span class="n">x</span><span class="o">=</span><span class="n">f</span><span class="p">[</span><span class="n">y</span><span class="o">=</span><span class="n">x</span><span class="p">])</span>
<span class="linenos" data-linenos="43 "></span>            <span class="n">splay</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="n">rc</span><span class="o">=</span><span class="n">y</span><span class="p">,</span><span class="n">pushup</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="44 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="45 "></span>    <span class="kt">void</span> <span class="nf">makeroot</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="46 "></span>        <span class="n">access</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="n">splay</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="47 "></span>        <span class="n">pushr</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="48 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="49 "></span>    <span class="kt">int</span> <span class="nf">findroot</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="50 "></span>        <span class="n">access</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="n">splay</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="51 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">lc</span><span class="p">)</span><span class="n">pushdown</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="n">x</span><span class="o">=</span><span class="n">lc</span><span class="p">;</span>
<span class="linenos" data-linenos="52 "></span>        <span class="n">splay</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="k">return</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="53 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="54 "></span>    <span class="kt">void</span> <span class="nf">split</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos="55 "></span>        <span class="n">makeroot</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="56 "></span>        <span class="n">access</span><span class="p">(</span><span class="n">y</span><span class="p">);</span><span class="n">splay</span><span class="p">(</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="57 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="58 "></span>    <span class="kt">void</span> <span class="nf">change</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">num</span><span class="p">){</span>
<span class="linenos" data-linenos="59 "></span>        <span class="n">splay</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="60 "></span>        <span class="n">v</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">num</span><span class="p">;</span>
<span class="linenos" data-linenos="61 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="62 "></span>    <span class="kt">int</span> <span class="nf">query</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos="63 "></span>        <span class="n">makeroot</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="64 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">findroot</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">!=</span><span class="n">x</span><span class="p">)</span><span class="k">return</span> <span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="65 "></span>        <span class="n">split</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="66 "></span>        <span class="k">return</span> <span class="n">s</span><span class="p">[</span><span class="n">y</span><span class="p">];</span>
<span class="linenos" data-linenos="67 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="68 "></span>    <span class="kt">bool</span> <span class="nf">link</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos="69 "></span>        <span class="n">makeroot</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="70 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">findroot</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">!=</span><span class="n">x</span><span class="p">){</span><span class="n">f</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">y</span><span class="p">;</span><span class="k">return</span> <span class="mi">1</span><span class="p">;}</span>
<span class="linenos" data-linenos="71 "></span>        <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="72 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="73 "></span>    <span class="kt">bool</span> <span class="nf">cut</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos="74 "></span>        <span class="n">makeroot</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="75 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">findroot</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">==</span><span class="n">x</span><span class="o">&amp;&amp;</span><span class="n">f</span><span class="p">[</span><span class="n">y</span><span class="p">]</span><span class="o">==</span><span class="n">x</span><span class="o">&amp;&amp;!</span><span class="n">c</span><span class="p">[</span><span class="n">y</span><span class="p">][</span><span class="mi">0</span><span class="p">]){</span>
<span class="linenos" data-linenos="76 "></span>            <span class="n">f</span><span class="p">[</span><span class="n">y</span><span class="p">]</span><span class="o">=</span><span class="n">c</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="77 "></span>            <span class="n">pushup</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="78 "></span>        <span class="p">}</span><span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="79 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="80 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
<h2 id="_15">可持久化平衡树</h2>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">N</span><span class="o">=</span><span class="mf">5e5</span><span class="o">+</span><span class="mi">100</span><span class="p">;</span>
<span class="linenos" data-linenos=" 2 "></span><span class="k">namespace</span> <span class="n">tree</span><span class="p">{</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="p">,</span><span class="n">rt</span><span class="p">[</span><span class="n">N</span><span class="p">],</span><span class="n">cnt</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="k">struct</span> <span class="nc">node</span><span class="p">{</span>
<span class="linenos" data-linenos=" 5 "></span>        <span class="kt">int</span> <span class="n">ch</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span>
<span class="linenos" data-linenos=" 6 "></span>        <span class="kt">int</span> <span class="n">rnd</span><span class="p">,</span><span class="n">sz</span><span class="p">,</span><span class="n">v</span><span class="p">;</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="c1">//rnd随机值，sz子节点的个数</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="c1">//cnt节点的个数 </span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="p">}</span><span class="n">t</span><span class="p">[</span><span class="n">N</span><span class="o">*</span><span class="mi">50</span><span class="p">];</span>
<span class="linenos" data-linenos="10 "></span>    <span class="kt">int</span> <span class="nf">cpynode</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="11 "></span>        <span class="n">t</span><span class="p">[</span><span class="o">++</span><span class="n">cnt</span><span class="p">]</span><span class="o">=</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">];</span>
<span class="linenos" data-linenos="12 "></span>        <span class="k">return</span> <span class="n">cnt</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="14 "></span>    <span class="kt">int</span> <span class="nf">newnode</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos="15 "></span>        <span class="n">t</span><span class="p">[</span><span class="o">++</span><span class="n">cnt</span><span class="p">].</span><span class="n">v</span><span class="o">=</span><span class="n">x</span><span class="p">;</span><span class="n">t</span><span class="p">[</span><span class="n">cnt</span><span class="p">].</span><span class="n">sz</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="16 "></span>        <span class="n">t</span><span class="p">[</span><span class="n">cnt</span><span class="p">].</span><span class="n">rnd</span><span class="o">=</span><span class="n">rand</span><span class="p">();</span><span class="k">return</span> <span class="n">cnt</span><span class="p">;</span>
<span class="linenos" data-linenos="17 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="18 "></span>    <span class="cp">#define ls(x) t[(x)].ch[0]</span>
<span class="linenos" data-linenos="19 "></span>    <span class="cp">#define rs(x) t[(x)].ch[1]</span>
<span class="linenos" data-linenos="20 "></span>    <span class="kt">void</span> <span class="nf">upd</span><span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="21 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
<span class="linenos" data-linenos="22 "></span>            <span class="n">t</span><span class="p">[</span><span class="n">k</span><span class="p">].</span><span class="n">sz</span><span class="o">=</span><span class="n">t</span><span class="p">[</span><span class="n">ls</span><span class="p">(</span><span class="n">k</span><span class="p">)].</span><span class="n">sz</span><span class="o">+</span><span class="n">t</span><span class="p">[</span><span class="n">rs</span><span class="p">(</span><span class="n">k</span><span class="p">)].</span><span class="n">sz</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="23 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="24 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">split</span><span class="p">(</span><span class="kt">int</span> <span class="n">now</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">,</span><span class="kt">int</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="o">&amp;</span><span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos="25 "></span>        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">now</span><span class="p">){</span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="k">return</span><span class="p">;}</span>
<span class="linenos" data-linenos="26 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">now</span><span class="p">].</span><span class="n">v</span><span class="o">&lt;=</span><span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="27 "></span>            <span class="n">x</span><span class="o">=</span><span class="n">cpynode</span><span class="p">(</span><span class="n">now</span><span class="p">);</span>
<span class="linenos" data-linenos="28 "></span>            <span class="n">split</span><span class="p">(</span><span class="n">rs</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="n">k</span><span class="p">,</span><span class="n">rs</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="29 "></span>        <span class="p">}</span><span class="k">else</span><span class="p">{</span>
<span class="linenos" data-linenos="30 "></span>            <span class="n">y</span><span class="o">=</span><span class="n">cpynode</span><span class="p">(</span><span class="n">now</span><span class="p">);</span>
<span class="linenos" data-linenos="31 "></span>            <span class="n">split</span><span class="p">(</span><span class="n">ls</span><span class="p">(</span><span class="n">y</span><span class="p">),</span><span class="n">k</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">ls</span><span class="p">(</span><span class="n">y</span><span class="p">));</span>
<span class="linenos" data-linenos="32 "></span>        <span class="p">}</span><span class="n">upd</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="n">upd</span><span class="p">(</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="33 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="34 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">merge</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos="35 "></span>        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">x</span><span class="o">||!</span><span class="n">y</span><span class="p">)</span><span class="k">return</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">;</span><span class="c1">//返回不为0的值</span>
<span class="linenos" data-linenos="36 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rnd</span><span class="o">&lt;</span><span class="n">t</span><span class="p">[</span><span class="n">y</span><span class="p">].</span><span class="n">rnd</span><span class="p">){</span>
<span class="linenos" data-linenos="37 "></span>            <span class="kt">int</span> <span class="n">z</span><span class="o">=</span><span class="n">cpynode</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="38 "></span>            <span class="n">rs</span><span class="p">(</span><span class="n">z</span><span class="p">)</span><span class="o">=</span><span class="n">merge</span><span class="p">(</span><span class="n">rs</span><span class="p">(</span><span class="n">z</span><span class="p">),</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="39 "></span>            <span class="n">upd</span><span class="p">(</span><span class="n">z</span><span class="p">);</span><span class="k">return</span> <span class="n">z</span><span class="p">;</span>
<span class="linenos" data-linenos="40 "></span>        <span class="p">}</span> <span class="k">else</span><span class="p">{</span>
<span class="linenos" data-linenos="41 "></span>            <span class="kt">int</span> <span class="n">z</span><span class="o">=</span><span class="n">cpynode</span><span class="p">(</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="42 "></span>            <span class="n">ls</span><span class="p">(</span><span class="n">z</span><span class="p">)</span><span class="o">=</span><span class="n">merge</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">ls</span><span class="p">(</span><span class="n">z</span><span class="p">));</span>
<span class="linenos" data-linenos="43 "></span>            <span class="n">upd</span><span class="p">(</span><span class="n">z</span><span class="p">);</span><span class="k">return</span> <span class="n">z</span><span class="p">;</span>
<span class="linenos" data-linenos="44 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="45 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="46 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">insert</span><span class="p">(</span><span class="kt">int</span> <span class="n">now</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="47 "></span>        <span class="kt">int</span> <span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">z</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="48 "></span>        <span class="n">split</span><span class="p">(</span><span class="n">rt</span><span class="p">[</span><span class="n">now</span><span class="p">],</span><span class="n">k</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="49 "></span>        <span class="n">z</span><span class="o">=</span><span class="n">newnode</span><span class="p">(</span><span class="n">k</span><span class="p">);</span>
<span class="linenos" data-linenos="50 "></span>        <span class="n">rt</span><span class="p">[</span><span class="n">now</span><span class="p">]</span><span class="o">=</span><span class="n">merge</span><span class="p">(</span><span class="n">merge</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">z</span><span class="p">),</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="51 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="52 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">del</span><span class="p">(</span><span class="kt">int</span> <span class="n">now</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="53 "></span>        <span class="kt">int</span> <span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">z</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="54 "></span>        <span class="n">split</span><span class="p">(</span><span class="n">rt</span><span class="p">[</span><span class="n">now</span><span class="p">],</span><span class="n">k</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">);</span><span class="c1">//先把大于k的结点分离</span>
<span class="linenos" data-linenos="55 "></span>        <span class="n">split</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">z</span><span class="p">);</span><span class="c1">//保证z中只有值为k的结点</span>
<span class="linenos" data-linenos="56 "></span>        <span class="n">z</span><span class="o">=</span><span class="n">merge</span><span class="p">(</span><span class="n">ls</span><span class="p">(</span><span class="n">z</span><span class="p">),</span><span class="n">rs</span><span class="p">(</span><span class="n">z</span><span class="p">));</span><span class="c1">//若值为k的结点有多个，则合并后少一    个，如果原本只有一个或者没有，则返回0，相当于删除了值为k的点</span>
<span class="linenos" data-linenos="57 "></span>        <span class="n">rt</span><span class="p">[</span><span class="n">now</span><span class="p">]</span><span class="o">=</span><span class="n">merge</span><span class="p">(</span><span class="n">merge</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">z</span><span class="p">),</span><span class="n">y</span><span class="p">);</span><span class="c1">//合并复原</span>
<span class="linenos" data-linenos="58 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="59 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">rnk</span><span class="p">(</span><span class="kt">int</span> <span class="n">now</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="60 "></span>        <span class="kt">int</span> <span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="61 "></span>        <span class="n">split</span><span class="p">(</span><span class="n">rt</span><span class="p">[</span><span class="n">now</span><span class="p">],</span><span class="n">k</span><span class="mi">-1</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">);</span><span class="c1">//以x为根的树中包含了所有值小于k的结点。</span>
<span class="linenos" data-linenos="62 "></span>        <span class="k">return</span> <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">sz</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span><span class="c1">//结点数+1就是k的排名</span>
<span class="linenos" data-linenos="63 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="64 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">kth</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="65 "></span>        <span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
<span class="linenos" data-linenos="66 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">ls</span><span class="p">(</span><span class="n">x</span><span class="p">)].</span><span class="n">sz</span><span class="o">+</span><span class="mi">1</span><span class="o">==</span><span class="n">k</span><span class="p">)</span><span class="k">return</span> <span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">v</span><span class="p">;</span>
<span class="linenos" data-linenos="67 "></span>            <span class="k">else</span> <span class="nf">if</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">ls</span><span class="p">(</span><span class="n">x</span><span class="p">)].</span><span class="n">sz</span><span class="o">&gt;=</span><span class="n">k</span><span class="p">)</span><span class="n">x</span><span class="o">=</span><span class="n">ls</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="68 "></span>            <span class="k">else</span> <span class="n">k</span><span class="o">-=</span><span class="n">t</span><span class="p">[</span><span class="n">ls</span><span class="p">(</span><span class="n">x</span><span class="p">)].</span><span class="n">sz</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">x</span><span class="o">=</span><span class="n">rs</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="69 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="70 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="71 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">pre</span><span class="p">(</span><span class="kt">int</span> <span class="n">now</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="72 "></span>        <span class="kt">int</span> <span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">z</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="73 "></span>        <span class="n">split</span><span class="p">(</span><span class="n">rt</span><span class="p">[</span><span class="n">now</span><span class="p">],</span><span class="n">k</span><span class="mi">-1</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="74 "></span>        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">x</span><span class="p">)</span><span class="k">return</span> <span class="mi">-2147483647</span><span class="p">;</span>
<span class="linenos" data-linenos="75 "></span>        <span class="k">return</span> <span class="nf">kth</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">t</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">sz</span><span class="p">);</span>
<span class="linenos" data-linenos="76 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="77 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">suf</span><span class="p">(</span><span class="kt">int</span> <span class="n">now</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="78 "></span>        <span class="kt">int</span> <span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">z</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="79 "></span>        <span class="n">split</span><span class="p">(</span><span class="n">rt</span><span class="p">[</span><span class="n">now</span><span class="p">],</span><span class="n">k</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">);</span>
<span class="linenos" data-linenos="80 "></span>        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">y</span><span class="p">)</span><span class="k">return</span> <span class="mi">2147483647</span><span class="p">;</span>
<span class="linenos" data-linenos="81 "></span>        <span class="k">return</span> <span class="nf">kth</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="82 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="83 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
			</div>
			<div class="panel panel-default card" style="padding: 10px;height: 50px;font-size: 20px;">
				<a style="float: right;" href="\article\How_to_Count.html"><i class="fa fa-angle-double-right"></i></a>
			</div>
			<div class="panel panel-default card" style="padding: 10px;font-size: 20px;" id="vcomments">
				
			</div>
			<script>
				new Valine({
					el: '#vcomments',
					appId: 'PAlFUPg0pQVTFTEo8gCB4BCf-gzGzoHsz',
					appKey: 'U38ejnLUiizJ6vLyp6ql4hRq',
					path: window.location.pathname
				})
			</script>
		</div>
		<div class="col-md-3">
				<div class="panel panel-default card">
				<div class="panel-heading">
					<h3 class="panel-title">
						<i class="fa fa-info"></i>&emsp;
						<strong>文章信息</strong>
					</h3>
				</div>
                <div style="margin: 10px;">
                    <div style="margin-top: 8px;display: flex;"> 
    <span style="flex: 1 0 auto;margin-right: 6px;">标题</span>
    <span><font style="font-weight: bold">[营业日志]菜鸡の模板</font></span>
	</div><div style="margin-top: 8px;display: flex;"> 
    <span style="flex: 1 0 auto;margin-right: 6px;">日期</span>
    <span>2020-12-26 18:30:00</span>
	</div>
                </div>
				</div>
				<div class="panel panel-default card">
				<div class="panel-heading">
					<h3 class="panel-title">
						<i class="fa fa-tag"></i>&emsp;
						<strong>标签</strong>
					</h3>
				</div>
				<div style="margin: 10px;">
					<a href="/tags/2020/" class="tag"><span  style="background-color: rgb(52, 152, 219);">2020</span></a>
				</div>
				</div>
				
			<div class="panel panel-default card">
					<div class="panel-heading">
						<h3 class="panel-title">
							<i class="fa fa-user-circle-o"></i>&emsp;
							<strong>caijicjl</strong>
						</h3>
					</div>
					<div style="width: 60%;display: inline-block;padding-top: 10px;">
						<ul class="propertyLinks" style="list-style-type: none;">
							<li>
								<img style="vertical-align:middle;position:relative;top:-2px" src="/img/rating-16x16.png">
								Rating:&nbsp;
								<span style="font-weight:bold;color: gray ">312</span>
							</li>
							<li>
								<img style="vertical-align:middle;position:relative;top:-2px" src="/img/star_blue_16.png">
								Contribution:&nbsp;
								<span style="color:gray;font-weight:bold;">0</span>
							</li>
						</ul>
						<ul class="nav-links">
							<li><a href="/settings/">Settings</a></li>
							<li><a href="/archives/">Blog</a></li>
							<li><a href="/teams">Teams</a></li>
							<li><a href="/submissions/dingdingsb">Submissions</a></li>
							<li><a href="/usertalk">Talks</a></li>
							<li><a href="/contests/with/dingdingsb">Contests</a></li>
						</ul>
					</div>
					<div style="display:inline-block;vertical-align: top;margin: 10px;text-align: center;">
						<div style="height:50px;width:50px"><img src="/img/avatar.png"/></div>
						<div><a href="https://www.luogu.com.cn/user/174304" style="font-weight:bold;color:gray">caijicjl</a></div>
					</div>
				</div>
		<div class="panel panel-default card">
				<div class="panel-heading">
					<h3 class="panel-title">
						<i class="fa fa-external-link"></i>&emsp;
						<strong>画中画</strong>
					</h3>
				</div>
				<div style="width:100%;margin:10px">
				<input type="text" id="url"/>
				<input value="创建" type="button" onclick="creat('kk')" /> 
				</div>
		</div>
		<script src="/js/hitokoto.js"></script>
							

				
				<span id="kk"></span>
				<div  id="myScrollspy" class="card" style="width: 100%;"  data-spy="affix">
					<script src="/js/make_toc.js"></script>
				</div>
		</div>
	</div>
 </div>
</body>
</html>
